使用Python解析ifconfig输出

2024-09-28 19:10:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试解析这个ifconfig输出。我看到了另一个堆栈溢出的例子,他们做了同样的代码,但它创建了一个嵌套列表。但是当我做同样的事情时,我只得到第一个匹配选项。另外,我想把RX和TX包添加到列表中,这似乎不起作用。在

Ifconfig输出

Mg0_RSP0_CPU0_0 Link encap:Ethernet  HWaddr 70:e4:22:32:53:42
          inet addr:20.200.130.1  Mask:255.255.0.0
          inet6 addr: fe80::72e4:22ff:fe32:5342/64 Scope:Link
          UP RUNNING NOARP MULTICAST  MTU:1514  Metric:1
          RX packets:147918 errors:0 dropped:0 overruns:0 frame:0
          TX packets:119226 errors:0 dropped:0 overruns:0 carrier:3
          collisions:0 txqueuelen:1000
          RX bytes:103741434 (98.9 MiB)  TX bytes:5320623 (5.0 MiB)

Tg0_0_0_7_0 Link encap:Ethernet  HWaddr 78:ba:f9:35:66:46
          inet addr:13.13.13.1  Mask:255.255.255.0
          inet6 addr: fe80::7aba:f9ff:fe35:6646/64 Scope:Link
          UP RUNNING NOARP MULTICAST  MTU:1514  Metric:1
          RX packets:26 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5058 errors:0 dropped:0 overruns:0 carrier:3
          collisions:0 txqueuelen:1000
          RX bytes:1832 (1.7 KiB)  TX bytes:454625 (443.9 KiB)

脚本

^{pr2}$

Tags: 列表byteslinkmaskrxaddrinetpackets
2条回答

好吧,我测试过你的代码,刚开始有一个结果,第二个结果是:

>>> ['Tg0_0_0_7_0', '13.13.13.1', '255.255.255.0']

然后,我仔细查看了正则表达式中的内容,似乎您可能在第二段之前有额外的新行,就像我在第一段之前那样,从而导致\S停止。你可以用(如果我对你得到单一结果的原因是正确的话)用添加\s?要开始正则表达式:

^{pr2}$

或者,如果是简单接口和IP检索,您可以使用更简单、更快的拆分…
如果有人好奇,我甚至会计时:

import timeit
import re

if_config_output = """
Mg0_RSP0_CPU0_0 Link encap:Ethernet  HWaddr 70:e4:22:32:53:42
          inet addr:20.200.130.1  Mask:255.255.0.0
          inet6 addr: fe80::72e4:22ff:fe32:5342/64 Scope:Link
          UP RUNNING NOARP MULTICAST  MTU:1514  Metric:1
          RX packets:147918 errors:0 dropped:0 overruns:0 frame:0
          TX packets:119226 errors:0 dropped:0 overruns:0 carrier:3
          collisions:0 txqueuelen:1000
          RX bytes:103741434 (98.9 MiB)  TX bytes:5320623 (5.0 MiB)

Tg0_0_0_7_0 Link encap:Ethernet  HWaddr 78:ba:f9:35:66:46
          inet addr:13.13.13.1  Mask:255.255.255.0
          inet6 addr: fe80::7aba:f9ff:fe35:6646/64 Scope:Link
          UP RUNNING NOARP MULTICAST  MTU:1514  Metric:1
          RX packets:26 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5058 errors:0 dropped:0 overruns:0 carrier:3
          collisions:0 txqueuelen:1000
          RX bytes:1832 (1.7 KiB)  TX bytes:454625 (443.9 KiB)
"""

ma = re.compile("^\s?(\S+).*?inet addr:(\S+).*?Mask:(\S+)", re.MULTILINE|re.DOTALL)

def split(paragraph):
    """ ugly, but faster """
    interface = paragraph.split(" Link ")[0]
    inet_mask = paragraph.split("\n")[1].split(':')
    ip, mask = inet_mask[1], inet_mask[2]
    return [interface, ip, mask]

def regex(paragraph):

    result = ma.match(paragraph)
    if result:
        result = ma.match(paragraph)
        interface = result.group(1)
        ip = result.group(2)
        mac = result.group(3)
        return [interface, ip, mac]

def test_split():
    c = []
    for paragraph in if_config_output.split('\n\n'):
        c.append(split(paragraph))
    return len(c)

def test_regex():
    c = []
    for paragraph in if_config_output.split('\n\n'):
        c.append(regex(paragraph))
    return len(c)

print ("split", timeit.timeit(stmt=test_split, number=100000))
print ("regex", timeit.timeit(stmt=test_regex, number=100000))

结果

$ python  version
Python 2.7.3
$ python test.py
('split', 3.096487045288086)
('regex', 5.066282033920288)
$ python3  version
Python 3.2.3
$ python3 test.py
split 4.155041933059692
regex 4.875624895095825
$ python3 test.py
split 4.787220001220703
regex 5.695119857788086

有人想加入Python3.5吗?在

嗯,奇怪的是没有结论。在

results from repl.it/languages/python3 (Python 3.4.0)
split 1.2351078800020332
regex 1.3363793969983817

results from ideone.com (Python 2.7.9) 
('split', 0.9004449844360352)
('regex', 0.7017428874969482)

and from ideone.com (Python 3.4.3+)
split 1.2050538789480925
regex 1.7611852046102285 

你的测量不正确。在函数中调用ma.match(paragraph)两次。在

result = ma.match(paragraph)
    if result:
        result = ma.match(paragraph)

Python 3.7.1(v3.7.1:260ec2c36a,2018年10月20日,03:13:28)
拆分0.569942316
雷克斯38852

相关问题 更多 >