簡體   English   中英

如果使用正則表達式沒有與此 ip 關聯,則匹配此名稱和 mac 地址

[英]Match this name and mac address if it is not associated with this ip using regex

我正在嘗試在下面的測試字符串示例中使用正則表達式匹配與 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)

使用正則表達式的一種方法是檢查名稱和地址之間是否找不到您的 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 : ether 后跟空格
  • ([\w:]+) :地址為字母數字字符和冒號的組合
  • ((?!.*172.16.1.102)[^\n]+\n)* :地址后面的任何內容
  • $ : 字符串結尾

您的信息將在:

  • 第 1 組 >> 名稱
  • 第 3 組 >> 地址

在這里試試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM