我正在嘗試在下面的測驗字串示例中使用正則運算式匹配與 ip 172.16.1.102 不相關的名稱和 MAC 地址。
在這種情況下,我想要的輸出是 (eth1, 00:e0:4c:68:ce:52)。因為“inet 172.16.1.102”與 eth0 和地址 d8:bb:c1:57:42:82 相關聯。
該名稱將是標準的 ubuntu 以太網配接器,因此在這種情況下不一定是 eth1。
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.1.102 netmask 255.255.255.0 broadcast 172.16.1.255
inet6 fe80::dabb:c1ff:fe57:4282 prefixlen 64 scopeid 0x20<link>
ether d8:bb:c1:57:42:82 txqueuelen 1000 (Ethernet)
RX packets 1103615 bytes 1565295587 (1.5 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1030880 bytes 799141588 (799.1 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 00:e0:4c:68:ce:52 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
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 1777991522 bytes 108254434164 (108.2 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1777991522 bytes 108254434164 (108.2 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wlan0: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether b8:08:cf:a0:7c:2e 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
舉個例子:我之前使用的python正則運算式匹配與ip 172.16.1.102關聯的名稱和mac地址:
.*((?<!\w)e\w ): flags.*(inet 172.16.1.102).*?(\w{2}[:]\w{2}[:]\w{2}[:]\w{2}[:]\w{2}[:]\w{2})
那么,如果有辦法在上面的運算式中說 not 'inet 172.16.1.102'?
示例 2:這個正則運算式也應該在這種情況下作業:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether d8:bb:c1:57:42:82 txqueuelen 1000 (Ethernet)
RX packets 1103615 bytes 1565295587 (1.5 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1030880 bytes 799141588 (799.1 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.16.1.102 netmask 255.255.255.0 broadcast 172.16.1.255
inet6 fe80::dabb:c1ff:fe57:4282 prefixlen 64 scopeid 0x20<link>
ether 00:e0:4c:68:ce:52 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
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 1777991522 bytes 108254434164 (108.2 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1777991522 bytes 108254434164 (108.2 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wlan0: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether b8:08:cf:a0:7c:2e 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
這個輸出應該是 (eth0, d8:bb:c1:57:42:82)
uj5u.com熱心網友回復:
使用正則運算式的一種方法是檢查名稱和地址之間是否找不到您的 ip:
^(eth\d )((?!.*172.16.1.102)[^\n] \n)*ether ([\w:] )((?!.*172.16.1.102)[^\n] \n)*$
解釋:
(eth\d ): 多于一位的 eth 單詞((?!.*172.16.1.102)[^\n] \n)*: 名字和地址之間的任何東西(?!.*172.16.1.102): 如果找到熱 ip,則不匹配的負前瞻[^\n]: 任何不同于新行的東西\n: 新隊
ether: 以太后跟空格([\w:] ): 地址為字母數字字符和冒號的組合((?!.*172.16.1.102)[^\n] \n)*: 地址后面的任何內容$: 字串結束
您的資訊將在:
- 第 1 組 >> 名稱
- 第 3 組 >> 地址
在這里試試。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/476794.html
