开发者

python extract from switch output

I have some info back from a LAN switch as below

Vlan 1 is administratively down, line protocol is down  
Vlan 2 is up, line protocol is up  
  Helper address is 192.168.0.2  
Vlan 3 is up, line protocol is up  
  Helper address is not set  
Vlan 4 is up, line protocol is up  
  Helper address is 192.168.0.2  
Vlan 5 is down, line protocol is down  
  Helper address is 192.168.0.2  
Vlan 6 is down, line protocol is down  
  Helper address is not set  
  Helper address is not 开发者_Python百科set

And the output I'm trying for is

Vlan 1,admin down,n/a
Vlan 2,up/up, 192.168.0.2
Vlan 3, up/up, not set
Vlan 4, up/up, 192.168.0.2
Vlan 5, down/down, 192.168.0.2
Vlan 6, down/down, not set

So the helper isn't always there (line 1) sometimes it's set sometimes it isn't, sometimes there are two lines (last Vlan - I only need 1)

and the Vlan can have states of admin down, up/up, up/down (not here) and down down.

So using Python and pexpect I can get the above output, but I'm having difficulty parsing out the consecutive lines. I've tried enumerate and then use key+1 for the next line, but the fact that there can be 0,1 or 2 lines following the Vlan screws me. Any ideas please?


import re

x="""
Vlan 1 is administratively down, line protocol is down  
Vlan 2 is up, line protocol is up  
  Helper address is 192.168.0.2  
Vlan 3 is up, line protocol is up  
  Helper address is not set  
Vlan 4 is up, line protocol is up  
  Helper address is 192.168.0.2  
Vlan 5 is down, line protocol is down  
  Helper address is 192.168.0.2  
Vlan 6 is down, line protocol is down  
  Helper address is not set  
  Helper address is not set
"""

x=x.replace(" is administratively down, line protocol is down  ",", admin down, n/a")
x=x.replace(" line protocol is ","")
x=x.replace(" is down,",", down/")
x=x.replace(" is up,",", up/")
x=re.sub("(?:\s*Helper address is (.*))+",", \\1",x)

print x

Vlan 1, admin down, n/a
Vlan 2, up/up, 192.168.0.2
Vlan 3, up/up, not set
Vlan 4, up/up, 192.168.0.2
Vlan 5, down/down, 192.168.0.2
Vlan 6, down/down, not set


Differentiate between lines of interest (which start with 'Vlan'; or not):

for line in lines:
    if line.startswith("Vlan"):
        # parse Vlanline
        # ...
    else:
        # parse data from helper line
        # ...


here's one way,

import re    
data=open("file").read()
r=re.split("\n[^ \t]+",data)
for i in r:
  print "-->",i.split("\n")

$ ./python.py
--> ['Vlan 1 is administratively down, line protocol is down  ']
--> [' 2 is up, line protocol is up  ', '  Helper address is 192.168.0.2  ']
--> [' 3 is up, line protocol is up  ', '  Helper address is not set  ']
--> [' 4 is up, line protocol is up  ', '  Helper address is 192.168.0.2  ']
--> [' 5 is down, line protocol is down  ', '  Helper address is 192.168.0.2  ']
--> [' 6 is down, line protocol is down  ', '  Helper address is not set  ', '  Helper address is not set', '']

now you can manipulate each item, since they are already grouped together


ghostdog gave me a clue to the solution

First I enumerated the table into a dictionary Then step through it. If the line began with VLAN I could then test for line+1 etc to see if it was a helper line

then output them all as one line and slice it up as I needed

Not the cleanest way, but works and thanks all for your help

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜