Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The awk command filters IPv4 addresses in the ifconfig command

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Foreword:

This article focuses on how to filter IPv4 addresses in the results of ifconfig commands.

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

The system for all cases in this chapter is Red Hat version 7.6.

[root@192 opt] # cat / etc/redhat-release Red Hat Enterprise Linux Server release 7.6 (Maipo)

Method 1:

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

[root@localhost] # ifconfigens33: flags=4163 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 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 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6:: 1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0B) 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 rules to filter out lines with IP addresses and wildcards to exclude addresses that begin with 127. finally, the valid IP address in the second column is printed.

Method 2:

Use the awk command in conjunction with the grep command to filter the IPv4 address.

[root@localhost] # ifconfigens33: flags=4163 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 ether 00:0c:29:d8:33:c3 txqueuelen 1000 (Ethernet) RX packets 24068 bytes 1711773 (1.6MiB) 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 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6:: 1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0B) 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 ($0Ev)}'| grep-RSTART "^ 127 | ^ $" 192.168.109.128

Note: first use the match command in awk combined with the rule to find out the position and length of the string in the IP address format, then use the substr command to intercept the IP address, and finally use the grep command to exclude the line starting with 127and the blank line and print out all other IP addresses.

Method 3:

Use the special value and regular matching of the RS variable in the awk command to filter out the IPv4 address.

[root@localhost] # ifconfigens33: flags=4163 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 ether 00:0c:29:d8:33:c3 txqueuelen 1000 (Ethernet) RX packets 24536 bytes 1748017 (1.6MiB) 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 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6:: 1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0B) 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: first 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 the location of the column where the IP address is located and print it out. This method is to ensure that the IP address is in the sixth column of the paragraph, of course, it can also be adjusted according to its own special circumstances.

RS variable: generally speaking, it is a newline delimiter variable with a default value of "\ n" newline character, while the RS variable determines how much data the awk command reads at one time. For example: RS= "" (space), the data read by awk at one time is a string that is no longer a line.

RS= "" (no spaces in the middle): this is a special value of the RS variable (separated by paragraphs), separated by blank lines or multiple consecutive empty behaviors, reading a piece of data at once.

Method 4:

Use the RS and FS variables in the awk command to filter out the ipv4 address (this method is complicated and mainly focuses on the use of awk)

[root@localhost] # ifconfigens33: flags=4163 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 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 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6:: 1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~] # ifconfig | awk 'BEGIN {RS= "" FS= "\ n"}! / ^ lo/ {$0 / 10 / FS = ""; $0 / 0 / print $2} '192.168.109.128

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

FS variable: the delimiter variable of the column. The value of the FS variable is reset once the contents of the $0 or column are reset, such as $0room2 or $2=xxx or $6room8, and so on.

Method 5:

Use a combination of awk and sed commands to filter IPv4 addresses.

[root@localhost] # ifconfigens33: flags=4163 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 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 0lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6:: 1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0B) 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 the sed command to filter out lines containing IP addresses, then use the awk command to exclude lines that contain IP addresses starting with 127. finally, print valid IP addresses for other lines.

Summary:

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

The main functions that involve the awk command are as follows:

RS variable: the row delimiter variable, which is the data read at once, is determined by this variable.

FS variable: column delimiter variable, the contents of each column are separated by this variable.

The match command: finds the position of the string in the matching format in each line, and the length of the string to which it is matched.

Substr command: intercepts data based on conditions. Command format: substr ("original data", "start position", "end position"), the location is mainly defined by numbers. If there is no end position, it is intercepted to the end of the original data.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 204

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report