python extract from switch output

Posted by household2 on Stack Overflow See other posts from Stack Overflow or by household2
Published on 2010-04-15T07:50:56Z Indexed on 2010/04/15 7:53 UTC
Read the original article Hit count: 157

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about python

Related posts about parse