2016-08-01 7 views
1

ループ内で1行ずつ読むことによって、Cisco show ip interface vrf allコマンド出力をtxtファイルから解析しようとしています。cisco cliコマンドの出力を正しく解析できません。

出力は次のようになります。

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) 

しかし、それは何のVLANを持っていないので、私はdefault vrfからの情報を必要としません。

私はこの情報を見つけるためにいくつかの正規表現を書いた:

vrf = re.findall(r'VRF "(.*)"', line) 
    vlan = re.findall(r'Vlan(\d+)', line) 
    subnet= re.findall(r'IP address.*subnet:\s(.*)$', line) 

をしかし、私はなしVLANでVRFを無視する方法がわかりません。 Pythonコードで

^(Vlan\d+).+[\n\r] 
.+?subnet:\ (.+) 


+0

最終結果は何ですか? – nicael

+0

私はすべてのvrfとそのvlan番号とサブネットを、mysqlに挿入する必要があります。 – Jegor

答えて

0

あなたはこの表現を使うことができ

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 


a demo on regex101.comを参照してください。

+0

それは働いていますが、私はまたvrf名を抽出する必要があります。 VRF \ "(。*)" [\ n \ r] Vlan(\ d +)。[[\ n \ r] 。+?サブネット:\(。+) vrfと最初のVLANと最初のサブネットがありますが、VRFには2つ以上のVLANがあります。 – Jegor

+0

@ Jegor:正確な出力を入力してください(元の質問を編集してください)。 – Jan

関連する問題