使用正则表达式匹配此名称和 mac 地址(如果它未与此 ip 相关联)

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 :以太后跟 space
  • ([\w:]+):地址为字母数字字符和冒号的组合
  • ((?!.*172.16.1.102)[^\n]+\n)*: 地址后的任何内容
  • $: 字符串结束

您的信息将在:

  • 第 1 组 >> 姓名
  • 第 3 组 >> 地址

试试看 here.