![](/img/trans.png)
[英]Python - Retrieve the output of Cisco CLI command and use it as input
[英]Can't correctly parse cisco cli command output
我试图通过循环读取逐行读取来自txt文件的Cisco show ip interface vrf all
命令输出。
输出是:
IP Interface Status for VRF "default"
loopback1, Interface status: protocol-up/link-up/admin-up, iod: 212,
IP address: 10.1.0.1, IP subnet: 10.1.0.0/30
...
Ethernet1/1, Interface status: protocol-up/link-up/admin-up, iod: 278,
IP address: 10.0.0.1, IP subnet: 10.0.0.0/30
IP Interface Status for VRF "TEST"
Vlan99, Interface status: protocol-up/link-up/admin-up, iod: 147,
IP address: 172.16.0.1, IP subnet: 172.16.0.0/24
...
Vlan888, Interface status: protocol-up/link-up/admin-up, iod: 115,
IP address: 172.16.1.1, IP subnet: 172.16.1.0/24
...
等等。
我只需要提取具有VLAN和子网的VRF。 例如,从这个输出中我应该在每次迭代中都有变量:
vrf -> vlan -> subnet (TEST -> 99 -> 172.16.0.0/24)
vrf -> vlan -> subnet (TEST -> 888 -> 172.16.1.0/24)
但我不需要来自default vrf
信息,因为它没有VLans。
我写了一些正则表达式来查找这个信息:
vrf = re.findall( r'VRF "(.*)"', line)
vlan = re.findall( r'Vlan(\d+)', line)
subnet= re.findall( r'IP address.*subnet:\s(.*)$', line)
但我不知道如何忽略没有vlans的VRF。
您可以使用此表达式:
^(Vlan\d+).+[\n\r]
.+?subnet:\ (.+)
Python
代码中:
import re rx = re.compile(""" ^(Vlan\\d+).+[\\n\\r] # capture Vlan, followed by digits at the beginning of line .+?subnet:\\ (.+) # match anything lazily, followed by subnet and capture the address """,re.MULTILINE|re.VERBOSE) for match in rx.finditer(your_string_here): print match.group(1) # Vlan99 print match.group(2) # the subnet ip
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.