[英]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.