简体   繁体   English

如何在python textfsm中结合2个正则表达式?

[英]How to combine 2 regular expression in python textfsm?

I have a textfsm python code which parses "ifconfig" command in Linux terminal 我有一个textfsm python代码,可以在Linux终端中解析“ ifconfig”命令

import textfsm

f = open('/home/Projects/test_ra/fsm_cli.txt')
inp = f.read()
f.close()

temp = open('/home/Projects/test_ra/fsm_cli.fsm')
tab = textfsm.TextFSM(temp)
res = tab.ParseText(inp)
print tab.header
for row in res:
    print row

In fsmcli.txt I have saved the "ifconfig" output 在fsmcli.txt中,我保存了“ ifconfig”输出

eth0      Link encap:Ethernet  HWaddr 48:4d:7e:9e:ab:b8  
          inet addr:172.23.100.200  Bcast:172.23.106.255 Mask:255.255.255.0
          inet6 addr: fe80::4a4d:7eaa:feaa:4eb8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1461589 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1576996 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1650010954 (1.6 GB)  TX bytes:1856124201 (1.8 GB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:11047 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11047 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1567815 (1.5 MB)  TX bytes:1567815 (1.5 MB)

vboxnet0  Link encap:Ethernet  HWaddr 0a:aa:27:00:00:00  
          inet addr:192.168.40.1  Bcast:192.168.33.255 Mask:255.255.255.0
          inet6 addr: fe80::800:27ff:fe00:0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:260 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:51436 (51.4 KB)

And I have written the fsm template -fsm_cli.fsm as below 而且我写了fsm模板-fsm_cli.fsm如下

Value Interface (\w+)
Value MAC (\S+)
Value Mask (\S+)
Value MTU (\d+)
Value IP (\S+)

Start
  ^${Interface}.*HWaddr\s+${MAC}
  ^\s+inet addr:${IP}.*Mask:${Mask}
  ^.*MTU:${MTU} -> Record

When I run the python file i get the below output: 当我运行python文件时,我得到以下输出:

['Interface', 'MAC', 'Mask', 'MTU', 'IP'] 
['eth0', '48:4d:7e:9e:4e:b8', '255.255.255.0', '1500', '172.23.106.239']
['', '', '255.0.0.0', '65536', '127.0.0.1']
['vboxnet0', '0a:00:27:00:00:00', '255.255.255.0', '1500', '192.168.33.1']

I'm not able to parse the "lo" interface (you could see the empty string in the third line of output). 我无法解析“ lo”接口(您可以在输出的第三行中看到空字符串)。 I know the reason because I have used "^${Interface}.*HWaddr\\s+${MAC}" in the fsm template. 我知道原因是因为我在fsm模板中使用了“ ^ $ {Interface}。* HWaddr \\ s + $ {MAC}”。 Since "lo" doesn't have Hwaddr it is not printing. 由于“ lo”没有Hwaddr,因此无法打印。 But how could I write a regular expression in fsm template to get the interface "lo" with blank HWaddr for it? 但是,如何在fsm模板中编写正则表达式以获取带有空白HWaddr的接口“ lo”呢?

^${Interface}.*(HWaddr\\s+${MAC}|Loopback) ^ $ {接口}。*(HWaddr \\ s + $ {MAC} |回送)

would be my guess. 是我的猜测。 That creates an alternative matching string. 这将创建一个替代匹配字符串。 put into regex101 https://regex101.com/r/lqzfFP/1 放入regex101 https://regex101.com/r/lqzfFP/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM