Python-Parse Ifconfig文件

Python-Parse Ifconfig文件,python,dictionary,Python,Dictionary,我正在尝试解析具有以下格式的ifconfig文件: Bond10G: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST> mtu 9000 inet 10.117.62.135 netmask 255.255.254.0 broadcast 10.117.63.255 ether 00:50:56:9e:89:10 txqueuelen 1000 (Ethernet) RX pa

我正在尝试解析具有以下格式的ifconfig文件:

Bond10G: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 9000
        inet 10.117.62.135  netmask 255.255.254.0  broadcast 10.117.63.255
        ether 00:50:56:9e:89:10  txqueuelen 1000  (Ethernet)
        RX packets 14315389  bytes 39499265855 (36.7 GiB)
        RX errors 0  dropped 35686  overruns 0  frame 0
        TX packets 13009616  bytes 38702751346 (36.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Bond1G: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 10.117.60.135  netmask 255.255.254.0  broadcast 10.117.61.255
        inet6 fe80::250:56ff:fe9e:ed0d  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:9e:ed:0d  txqueuelen 1000  (Ethernet)
        RX packets 1573455  bytes 172628399 (164.6 MiB)
        RX errors 0  dropped 10946  overruns 0  frame 0
        TX packets 185449  bytes 50369231 (48.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 9000
        ether 00:50:56:9e:89:10  txqueuelen 1000  (Ethernet)
        RX packets 13493291  bytes 39433797198 (36.7 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 13006856  bytes 38701854528 (36.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 9000
        ether 00:50:56:9e:89:10  txqueuelen 1000  (Ethernet)
        RX packets 822097  bytes 65468597 (62.4 MiB)
        RX errors 0  dropped 35673  overruns 0  frame 0
        TX packets 2760  bytes 896818 (875.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:50:56:9e:ed:0d  txqueuelen 1000  (Ethernet)
        RX packets 961003  bytes 127916200 (121.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 182704  bytes 49477386 (47.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth3: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 00:50:56:9e:ed:0d  txqueuelen 1000  (Ethernet)
        RX packets 612452  bytes 44712199 (42.6 MiB)
        RX errors 0  dropped 10930  overruns 0  frame 0
        TX packets 2745  bytes 891845 (870.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 3164912  bytes 12725232051 (11.8 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3164912  bytes 12725232051 (11.8 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
我已经能够通过新线分隔每个接口,但是我不确定如何继续。示例代码:

with open('ifconfig_file) as data:
    for line in data:
        temp_array = line.split("\n\n")
我的逻辑是(如果我错了,请纠正我):

  • 按冒号拆分以获取接口名称以查找密钥(问题是以太中有冒号)

  • 虽然不是空行,但采用由空格分隔的值,数组[0]将是键,数组[1]将是值


  • 您可以利用
    split()
    以及正则表达式。下面是一个示例,我解析
    mtu
    inet

    重新导入
    def解析_信息(接口):
    信息={}
    mtuMatches=re.findall(“mtu[0-9]*\s”,接口)#查找mtu的所有匹配项
    如果(len(mtuMatches)>0:
    信息['mtu']=mtu匹配[0]。替换(“mtu”,”)。strip()#使用第一个匹配项
    inetMatches=re.findall(“inet[0-9]{1,3}\[0-9]{1,3}\[0-9]{1,3}\[0-9]{1,3}\[0-9]{1,3}\s”,接口)#查找inet的所有匹配项
    如果(len(inetMatches)>0):
    信息['inet']=inetMatches[0]。替换(“inet”,”)。strip()#使用第一个匹配项
    #在这里添加更多
    返回信息
    def解析_名称(接口):
    部件=接口。拆分(“:”)
    返回部件[0]#获取名称
    def解析_接口(接口):
    名称=解析名称(接口)
    信息=解析信息(接口)
    返回名称、信息
    def parse_文件(数据):
    接口=数据。拆分(“\n\n”)
    已解析={}
    对于接口中的接口:
    名称,info=parse_接口(接口)
    已解析[名称]=信息
    返回解析
    打开('ifconfig.txt')作为文件:
    打印(解析_文件(file.read()))
    
    这可能不是最有效的方法(如果您想要性能,那么可以按空间分割并迭代结果),但在我看来,这是最干净的方法

    with open('ifconfig_file) as data:
        for line in data:
            temp_array = line.split("\n\n")