您好,登錄后才能下訂單哦!
前言:
????????本文主要講解如何過濾ifconfig命令結果中的IPv4地址。
????????本文章的案例適合查找出所有網卡的IPv4地址。
????????本章所有案例的系統是Red Hat 7.6版本。
[root@192?opt]#?cat?/etc/redhat-release? Red?Hat?Enterprise?Linux?Server?release?7.6?(Maipo)
方法一:
? ? ? ? 使用awk命令、正則表達式、通配符來篩選IPv4地址。
[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
????????注釋:awk命令用正則來過濾出有IP地址的行并且用通配符來排除以127開頭(回環地址)的地址,最后打印出第二列的有效IP地址。
方法二:
????? ? 結合使用awk命令和grep命令來過濾IPv4地址。
[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
????????注釋:先使用awk中match命令結合正則查找出IP地址格式的字符串所在行的位置和長度,再用substr命令來截取IP地址,最后使用grep命令排除以127開頭(回環地址)和空行并打印出其它所有的IP地址。
方法三:
????????使用awk命令中RS變量的特殊值和正則匹配來篩選出IPv4地址。
[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
????????注釋:先使用BEGIN來定義RS變量的值為段落分隔(BEGIN{RS=""}),然后用正則匹配來排除以lo開頭的段落,最后找出IP地址所在列的位置并打印出來。本方法要確保IP地址要在段落的第六列,當然也可以根據自己的特殊情況來進行調整。
????????RS變量:通俗講就是換行分隔符變量,默認值為"\n"換行符,而awk命令一次性讀取多少數據都是由RS變量來決定的。例如:RS=" "(空格),awk一次性讀取的數據為一個字符串 不再是一行的數據了。
????????RS=""(中間沒有空格):此為RS變量的特殊值(段落分隔),以空行或多個連續空行為分隔,一次性讀取一段數據。
方法四:
????????使用awk命令中的RS和FS變量來篩選出ipv4地址(本方法比較復雜,主要講的是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
????????注釋:先使用BEGIN定義awk以段落來讀取數據,列以換行符來分隔(BEGIN{RS="";FS="\n"})。再使用正則匹配排除以lo開頭的段落。重新設置$0的值為$2(含有IP地址的那一行內容,此時列的分隔符為換行符),設置FS變量的值為空格(FS=" ",列分隔符為空格),再重新設置$0的值但保持內容不變($0=$0,重置FS的值),最后打印出IP地址。
????????FS變量:列的分隔符變量。$0=$2或$2=xxx或$6=$8等等只要重置了$0或列的內容都會重置一次FS變量的值。
方法五:
????????結合使用awk和sed命令來過濾IPv4地址。
[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
????????注釋:先用sed命令過濾出包含IP地址的行,再使用awk命令排除含有以127開頭(回環地址)IP地址的行,最后打印其他行的有效的IP地址。
總結:
????????本文章主要使用awk命令來篩選IPv4地址。
????????涉及到awk命令的功能主要如下所示:
????????????????RS變量:行分隔符變量,就是一次性讀取的數據由此變量來決定。
????????????????FS變量:列分隔符變量,每一列的內容由此變量來進行分隔。
????????????????match命令:查找所匹配格式的字符串在每行中的位置,以及所匹配到的字符串的長度。
????????????????substr命令:根據條件來截取數據。命令格式:substr("原始數據","開始位置","結束位置"),位置主要以數字來定義。沒有結束位置則截取到原始數據的末尾。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。