python正则表达式练习--输入设备和名称,查找对应值

日志文件: 

enp0s20u1u5: flags=4163  mtu 1500
        inet 169.254.95.120  netmask 255.255.255.0  broadcast 169.254.95.255
        inet6 fe80::894:efff:fe51:1529  prefixlen 64  scopeid 0x20
        ether 0a:94:ef:51:15:29  txqueuelen 1000  (Ethernet)
        RX packets 13032294  bytes 1070708969 (1021.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12993075  bytes 1249591951 (1.1 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens3f1: flags=4099  mtu 1500
        ether 3c:fd:fe:51:e3:31  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

ens4f0: flags=4099  mtu 1500
        ether a0:36:9f:e0:df:78  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
        device memory 0x90a00000-90afffff  

ens4f1: flags=4099  mtu 1500
        ether a0:36:9f:e0:df:79  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
        device memory 0x90b00000-90bfffff  

eth0: flags=4163  mtu 1500
        inet 192.168.45.170  netmask 255.255.255.224  broadcast 10.194.45.191
        inet6 fe80::3efd:feff:fe51:e330  prefixlen 64  scopeid 0x20
        ether 3c:fd:fe:51:e3:30  txqueuelen 1000  (Ethernet)
        RX packets 6760100909  bytes 9548292146551 (8.6 TiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 715984152  bytes 95933726994 (89.3 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 0  (Local Loopback)
        RX packets 30825  bytes 85384315 (81.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30825  bytes 85384315 (81.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

代码:

import re


def get_dev_info(dev, item):
    #打开日志文件
    try:
        f = open('D:\secureCRT_Log\inet.log','r')
    except IOError:
        print('文件打开失败')
    str1 = f.read()
    #先检索出该设备及其对应的信息
    pattern1 = r'{}.*?\n\n'.format(dev)
    obj = re.compile(pattern=pattern1,flags=re.DOTALL)
    str2 = str(obj.findall(str1))
    #第二步,从设备信息中,根据item 获取具体的值
    pattern2 = r'\s+'
    obj = re.compile(pattern=pattern2)
    #根据空字符串拆分,获取结果列表
    lst = obj.split(str2)
    # 值的列表下标是item + 1
    idx = lst.index(item) + 1
    return lst[idx]


def main():
    #根据不同的DEV和ITEM获取具体值
    DEV = 'eth0'
    ITEM = 'inet'
    address = get_dev_info(DEV,ITEM)
    print(address)
    DEV = 'ens3f1'
    ITEM = 'ether'
    address = get_dev_info(DEV,ITEM)
    print(address)
    DEV = 'enp0s20u1u5'
    ITEM = 'inet6'
    address = get_dev_info(DEV,ITEM)
    print(address)

main()

运行结果:

192.168.45.170
3c:fd:fe:51:e3:31
fe80::894:efff:fe51:1529

Process finished with exit code 0

 

你可能感兴趣的:(python正则表达式练习--输入设备和名称,查找对应值)