#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep Power
    0

    Red face 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
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep Power
    0
    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
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    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!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep Power
    0
    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.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    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!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    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: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0
    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
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep Power
    0
    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.

IMN logo majestic logo threadwatch logo seochat tools logo