If you display the ip with ifconfig, you can get the ip of multiple NICs. For example, it looks like the following. Think about how to get the NIC address of eth0 with ifconfig.
$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.28.99.150 netmask 255.255.240.0 broadcast 172.28.111.255
inet6 fe80::215:5dff:xxx:xxxx prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:90:90:41 txqueuelen 1000 (Ethernet)
RX packets 742 bytes 149376 (149.3 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 16 bytes 1216 (1.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
To gerp with ifconfig and get only the IP address, do as follows.
ifconfig eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
Display the information of the NIC you want to get with ifcongit eth0. If you want to get the IP address of another NIC, specify the number like ethXX. (Is this the case ...) Then I pipe the result and grep it with a regular expression. o is an option to take only the matches, P is a Perl-style regular expression, With this combination, you can create a part where row extraction and clipping match.
Recommended Posts