The awk command filters IPv4 addresses in the ifconfig command

Posted by jl on Thu, 12 Dec 2019 19:51:08 +0100

Preface:

This article mainly explains how to filter IPv4 addresses in the results of the ifconfig command.

The case in this article is suitable for finding IPv4 addresses for all network cards.

Method 1:

Use awk commands, regular expressions, and wildcards to filter IPv4 addresses.

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.109.128  netmask 255.255.255.0  broadcast 192.168.109.255
        inet6 fe80::20c:29ff:fed8:33c3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d8:33:c3  txqueuelen 1000  (Ethernet)
        RX packets 22836  bytes 1608781 (1.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6159  bytes 617125 (602.6 KiB)
        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

[root@localhost ~]# ifconfig|awk '/inet / && $2 !~ /^127/{print $2}'
192.168.109.128

Note: The awk command uses regularity to filter out rows with IP addresses and wildcards to exclude addresses starting with 127 (loopback address), and finally prints out the valid IP addresses in column 2.

Method 2:

Combine awk and grep commands to filter IPv4 addresses.

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.109.128  netmask 255.255.255.0  broadcast 192.168.109.255
        inet6 fe80::20c:29ff:fed8:33c3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d8:33:c3  txqueuelen 1000  (Ethernet)
        RX packets 24068  bytes 1711773 (1.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6769  bytes 690417 (674.2 KiB)
        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

[root@localhost ~]# ifconfig|awk '{match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/);print substr($0,RSTART,RLENGTH)}'|grep -Ev "^127|^$"
192.168.109.128

Note: Use the match command in awk to find out the position and length of the line where the string in IP address format is located, then use the substr command to intercept the IP address, and use the grep command to exclude the beginning of 127 (loopback address) and blank lines and print out all other IP addresses.

Method 3:

Use the special values and regular matches of the RS variable in the awk command to filter out IPv4 addresses.

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.109.128  netmask 255.255.255.0  broadcast 192.168.109.255
        inet6 fe80::20c:29ff:fed8:33c3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d8:33:c3  txqueuelen 1000  (Ethernet)
        RX packets 24536  bytes 1748017 (1.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6958  bytes 712613 (695.9 KiB)
        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

[root@localhost ~]# ifconfig|awk 'BEGIN{RS=""}!/^lo/{print $6}'
192.168.109.128

Note: Use BEGIN to define the value of the RS variable as paragraph delimited (BEGIN{RS="}), then use regular matching to exclude paragraphs that start with lo, and finally find out where the IP address is in the column and print it out.This method ensures that the IP address is in the sixth column of the paragraph, but it can also be adjusted according to its own special circumstances.

RS variable: Generally speaking, it is the line breaker variable, the default value is the'\n'line break, and how much data the awk command reads at once is determined by the RS variable.For example, RS= "" (space), awk reads one-time data as a string and is no longer a row of data.

RS="(no space in the middle): This is a special value of an RS variable (paragraph-delimited), separated by blank lines or multiple consecutive empty behaviors, that reads a piece of data at once.

Method 4:

* Use RS and FS variables in the awk command to filter out ipv4 addresses (this method is more complex, mainly the use of awk)

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.109.128  netmask 255.255.255.0  broadcast 192.168.109.255
        inet6 fe80::20c:29ff:fed8:33c3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d8:33:c3  txqueuelen 1000  (Ethernet)
        RX packets 25278  bytes 1804185 (1.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7240  bytes 745239 (727.7 KiB)
        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

[root@localhost ~]# ifconfig|awk 'BEGIN{RS="";FS="\n"}!/^lo/{$0=$2;FS=" ";$0=$0;print $2}'
192.168.109.128

Note: Use BEGIN to define awk to read data in paragraphs and columns separated by line breaks (BEGIN{RS="; FS="\n"}).Then use regular matching to exclude paragraphs that start with lo.Reset the value of $0 to $2 (the line containing the IP address, where the column separator is a line break), set the value of the FS variable to a space (FS=", the column separator is a space), reset the value of $0 but leave the content unchanged ($0=$0, reset the value of FS), and print out the IP address.

FS variable: The delimiter variable for a column.$0=$2 or $2=xxx or $6=$8, and so on, will reset the value of the FS variable once whenever the contents of $0 or column are reset.

Method 5:

Combine awk and sed commands to filter IPv4 addresses.

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.109.128  netmask 255.255.255.0  broadcast 192.168.109.255
        inet6 fe80::20c:29ff:fed8:33c3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d8:33:c3  txqueuelen 1000  (Ethernet)
        RX packets 26232  bytes 1879377 (1.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7636  bytes 791883 (773.3 KiB)
        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

[root@localhost ~]# ifconfig|sed -n '/inet /p'|awk '$2 !~ /^127/{print $2}'
192.168.109.128

Note: Use sed to filter out rows containing IP addresses, awk to exclude rows containing IP addresses starting with 127 (loopback address), and print valid IP addresses for other rows.

Summary:

This article mainly uses the awk command to filter IPv4 addresses.

The functions related to awk commands are as follows:

RS variable: The row delimiter variable is the data that is read at one time and is determined by this variable.

FS variable: The column delimiter variable by which the contents of each column are separated.

The match command: Find the position of the matched format string in each line and the length of the matched string.

Substir command: intercept data based on conditions.Command format: substr("raw data", "start position", "end position"), position is mainly defined by number.Without an end position, it is intercepted to the end of the original data.


Topics: Linux network