views:

49

answers:

4

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 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?

A: 

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
        # ...
ChristopheD
+1  A: 

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

ghostdog74
this worked with a little tweaking of the input - there can be 0,1 or 2 helper lines
+1  A: 
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
S.Mark
A: 

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