91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

awk命令過濾ifconfig命令中的IPv4地址

發布時間:2020-07-02 01:29:35 來源:網絡 閱讀:898 作者:heian95tianshi 欄目:系統運維

前言:

????????本文主要講解如何過濾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("原始數據","開始位置","結束位置"),位置主要以數字來定義。沒有結束位置則截取到原始數據的末尾。


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

方正县| 大厂| 临朐县| 海兴县| 宣化县| 阿坝| 唐河县| 农安县| 威信县| 永康市| 平利县| 黑山县| 深圳市| 永平县| 山西省| 扬州市| 衡东县| 米林县| 冕宁县| 榆中县| 永清县| 普洱| 龙里县| 永靖县| 潮州市| 巴东县| 白玉县| 濮阳县| 石门县| 孙吴县| 石泉县| 于都县| 临西县| 县级市| 罗江县| 满城县| 河南省| 星座| 偃师市| 蓝田县| 鄂伦春自治旗|