August 20th, 2012, 08:29 PM
-
Take string out of a non-standard list
I'm doing an script that ssh into a cisco device send a command and collet the output. I would like to use the output in an spreadsheet, so when I get the output I have to filter out the way I want. The following is the output that I receive from my device:
Code:
Port Name Status Vlan Duplex Speed Type
Gi0/1 end user notconnect 101 auto auto 10/100/1000BaseTX
Gi0/2 Connected to center connected 101 a-full a-100 10/100/1000BaseTX
Gi0/3 Connection to LA connected 10 a-full a-1000 10/100/1000BaseTX
Gi0/4 Connection to SAT connected 501 full 100 10/100/1000BaseTX
Gi0/5 XP Server connected 101 a-full a-1000 10/100/1000BaseTX
Gi0/6 MAC Server connected 101 a-full a-100 10/100/1000BaseTX
Gi0/7 IDF connected 101 half 10 10/100/1000BaseTX
Gi0/8 User Ports connected 101 a-full a-100 10/100/1000BaseTX
Gi0/9 User Ports connected 101 a-full a-100 10/100/1000BaseTX
I want to assigned those output to a variable:
Code:
list = [the list on the top]
print list [1] #this is an example, so you guys can see what I get
for i in lisy:
q,w,e,r,t,y,u,i=i.strip().split()
As you can see the list is separate by space, so in my first iten in the list are port, name, status, vlan, duplex, speed and type. 7 variable needed, but the second I need 8 because of the space. How can I make the list more concistent where everything under port, name, status,vlan, duplex, speed and type be one variable. I just want to able to do a with 7 variable. Thanks
August 20th, 2012, 08:37 PM
-
Let say that at the end I do:
print w
I would like to have the following output:
name
end user
connected to center
connection to LA
connection to SAT
XP Server
IDF
User Ports
User Ports
August 20th, 2012, 09:07 PM
-
If the second field is the only one having spaces python3 has the easiest method:
Code:
$ python3
Python 3.2.3 (default, May 3 2012, 15:51:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> (Port, *Name, Status, Vlan, Duplex, Speed,Type,)='Gi0/1 end user notconnect 101 auto auto 10/100/1000BaseTX'.split()
>>> Port
'Gi0/1'
>>> Name
['end', 'user']
>>> Status
'notconnect'
>>> Vlan
'101'
>>> Duplex
'auto'
>>> Speed
'auto'
>>> Type
'10/100/1000BaseTX'
>>>
otherwise you could split the fields based on width, which is a little strange, I mark the fields as
Code:
< < < < > > <
Port Name Status Vlan Duplex Speed Type
Gi0/1 end user notconnect 101 auto auto 10/100/1000BaseTX
with the angle brackets marking a field start or end.
Finally, this works in python2 and 3 and depends on only the one field having spaces, you index from both ends, example:
Code:
$ python
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a='Gi0/1 end user notconnect 101 auto auto 10/100/1000BaseTX'.split()
>>> a
['Gi0/1', 'end', 'user', 'notconnect', '101', 'auto', 'auto', '10/100/1000BaseTX']
>>> Port = a[0]
>>> (Status,Vlan,Duplex,Speed,Type,) = a[-5:]
>>> Status
'notconnect'
>>> Name = ' '.join(a[1:-5])
>>> Name
'end user'
>>>
[code]
Code tags[/code] are essential for python code and Makefiles!
August 20th, 2012, 09:41 PM
-
I don't understand how you do it based on width. Can you explain little bit more. The output that I used is of an know device, the devices that I will be running, I would not know how the output is going to be.
August 20th, 2012, 10:07 PM
-
Code:
>>> header='Port Name Status Vlan Duplex Speed Type'
>>> indexes = [header.index(a) for a in header.split()]
>>> i = iter(indexes)
>>> next(i)
0
>>> slices = [slice(*a) for a in zip(indexes,i)]
>>> slices
[slice(0, 10, None), slice(10, 30, None), slice(30, 43, None), slice(43, 54, None), slice(54, 62, None), slice(62, 68, None)]
>>> ['Gi0/1 end user notconnect 101 auto auto 10/100/1000BaseTX'[s] for s in slices]
['Gi0/1 ', 'end user ', 'notconnect ', '101 ', ' auto ', ' auto ']
>>>
No, I'm just playing. Do you have more information about identifying the Vlan and Duplex fields?
[code]
Code tags[/code] are essential for python code and Makefiles!
August 21st, 2012, 05:22 AM
-
I’m a little confused... If you intend to use the output in a spreadsheet, as you say, why trouble with parsing the file? Every spreadsheet program I know can read CSV data also in a fixed-width column format.
My armada: Debian GNU/Linux 8 (desktop, home laptop, work laptop), Raspbian GNU/Linux 8 (nameserver), Ubuntu 14.04.3 LTS (HTPC), PC-BSD 10.2 (testbed), Android 4.2.1 (tablet)
August 21st, 2012, 12:34 PM
-
What are the expected values of Status. If it is a well known string ( i guess it should be since it is the output from a router), then try using a regular expression to strip out the string between "Port" and the "Status". that should give you the required field
August 22nd, 2012, 10:38 AM
-
Originally Posted by b49P23TIvg
Code:
>>> header='Port Name Status Vlan Duplex Speed Type'
>>> indexes = [header.index(a) for a in header.split()]
>>> i = iter(indexes)
>>> next(i)
0
>>> slices = [slice(*a) for a in zip(indexes,i)]
>>> slices
[slice(0, 10, None), slice(10, 30, None), slice(30, 43, None), slice(43, 54, None), slice(54, 62, None), slice(62, 68, None)]
>>> ['Gi0/1 end user notconnect 101 auto auto 10/100/1000BaseTX'[s] for s in slices]
['Gi0/1 ', 'end user ', 'notconnect ', '101 ', ' auto ', ' auto ']
>>>
No, I'm just playing. Do you have more information about identifying the Vlan and Duplex fields?
This work, so I was able to do this.