IP address extraction

Keywords: network

IP address extraction

ifconfig can print out the information of all local network interfaces, including the ip address of the interface.
The display is as follows:

[root@desktop01 ~ 18:45:56 130]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.147.150  netmask 255.255.255.0  broadcast 192.168.147.255
        inet6 fe80::8b99:5e36:1259:15a4  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1f:d9:ee  txqueuelen 1000  (Ethernet)
        RX packets 121312  bytes 116090271 (110.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33085  bytes 8208510 (7.8 MiB)
        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 1411  bytes 121142 (118.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1411  bytes 121142 (118.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:44:b9:fb  txqueuelen 1000  (Ethernet)
        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

The details of interface ens33 are as follows:

[root@desktop01 ~ 18:46:01 131]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.147.150  netmask 255.255.255.0  broadcast 192.168.147.255
        inet6 fe80::8b99:5e36:1259:15a4  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1f:d9:ee  txqueuelen 1000  (Ethernet)
        RX packets 121509  bytes 116102974 (110.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33104  bytes 8211668 (7.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Now extract the ip address from it.
In this paper, grep, awk and sed are three text processing tools to complete the work.

Train of thought I

  • Use grep to extract the line containing the ip address, and then use sed to edit the line. The command is as follows:
ifconfig ens33 | grep "inet " | sed '{s/inet//g}' | sed '{s/netmask.*//g}' | sed '{s/ *//g}'

First, use grep "inet" to extract the line containing ip address information, that is
inet 192.168.147.150 netmask 255.255.255.0 broadcast 192.168.147.255
sed '{s/inet//g}' delete the inet of the row (replace with empty, which means delete)
sed '{s/netmask.*//g}' delete this line of netmask and all characters after it
sed '{s/ *//g}' means to delete the space before ip address

Train of thought II

First edit the whole output with sed, delete the unnecessary lines, and then continue to edit the lines containing the ip address.
The command is as follows:

ifconfig ens33 | sed 1d | sed '2,$d' | sed 's/inet //g' | sed 's/netmask.*//g' | sed 's/ *//g'

It can also be written as:

ifconfig ens33 | sed 1d | sed '2,$d' | sed '{s/inet//g;s/netmask.*//g;s/ *//g}'

sed 1d delete the first line and get the following results:

[root@desktop01 ~ 18:58:28 132]# ifconfig ens33 | sed 1d
        inet 192.168.147.150  netmask 255.255.255.0  broadcast 192.168.147.255
        inet6 fe80::8b99:5e36:1259:15a4  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1f:d9:ee  txqueuelen 1000  (Ethernet)
        RX packets 121628  bytes 116111077 (110.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33131  bytes 8214682 (7.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

sed '2,$d' delete the second line to the last line of the above results and get the following results:

[root@desktop01 ~ 19:04:24 133]# ifconfig ens33 | sed 1d | sed '2,$d'
        inet 192.168.147.150  netmask 255.255.255.0  broadcast 192.168.147.255

The second half of the order is the same idea

Train of thought 3

With awk, the command is as follows:

ifconfig | awk -F [" ":]+ 'NR==2 {print $4}'

-F ["":] + specifies that the field separator is at least one space or colon
NR==2 matches the second line
{print $4} output the fourth field, ip address

Posted by rickbond79 on Mon, 16 Dec 2019 14:59:19 -0800