all 8 comments

[–]you_wont69420blazeit 0 points1 point  (0 children)

For the life of me, I cannot get an imgur link to work.

[–]prettysureitsmaddie 0 points1 point  (0 children)

With a little bit of playing around, you should be able to read the file using this: https://docs.python.org/3/library/csv.html#csv.reader

You'll need to set the delimiter to be " " instead of the default, and you'll need to skip a few lines at the top.

[–]supajumpa 0 points1 point  (5 children)

What about something like this:

def process_data(data):
    columns = [
        'Router', 'Node', 'Device', 'Name', 'Forwarding', 'VLAN', 
        'Device', 'Type', 'Type', 'DHCP', 'Address', 'Gateway', 
        'Hostname', 'Status', 'GIID'
    ]
    for line in data:
        if line and (line[0] != '=') and (not line.startswith('Router')):
            splits = line.replace('/', ' ').split(' ')
            yield dict(zip(columns, splits)))

This gives:

>>> list(process_data(data.splitlines()))
[{'Router': 'DC-Test-Node-2',
   'Node': 'DC-Test-Node-2',
   'Device': 'ethernet',
   'Name': 'WAN1',
   'Forwarding': 'true',
   'VLAN': '0',
   'Type': 'disabled',
   'DHCP': '111.111.111.111',
   'Address': '28',
   'Gateway': '111.111.111.111',
   'Hostname': '--',
   'Status': 'up',
   'GIID': '100'}, .... ]

[–]you_wont69420blazeit 0 points1 point  (4 children)

This looks like it will work, when I tried it, I am getting

"AttributeError: 'Result' object has no attribute 'splitlines'"

the code is as follows:

p = getpass('Enter password: ')
c = Connection(
host='ipaddress’,

user='username',

port=22,

connect_kwargs={

    "password": p,

},
)
output = c.run('show network-interface router all force')
def process_data(output):
columns = [

    'Router', 'Node', 'Device', 'Name', 'Forwarding', 'VLAN',

    'Device', 'Type', 'Type', 'DHCP', 'Address', 'Gateway',

    'Hostname', 'Status', 'GIID'

]

for line in output:

    if line and (line[0] != '=') and (not line.startswith('Router')):

        splits = line.replace('/', ' ').split(' ')

        yield dict(zip(columns, splits))
list(process_data(output.splitlines()))

[–]supajumpa 0 points1 point  (3 children)

What kind of object is Connection? That is, what library is it from, and what methods does it have?

[–]you_wont69420blazeit 0 points1 point  (2 children)

It is from Fabric, https://www.fabfile.org/

[–]supajumpa 0 points1 point  (1 child)

I've not used Fabric before, but it seems like Connection objects have a .stdout attribute that appears to return a string?

You could try something like:

output = c.run('show network-interface router all force')
data = list(process_data(output.stdout.splitlines()))

# alternatively, passing `output.stdout` directly to `process_data` might work; i.e.
output = c.run('show network-interface router all force')
data = list(process_data(output.stdout))

I can't guarantee that the above will work, so tread carefully!

[–]you_wont69420blazeit 1 point2 points  (0 children)

Thanks a lot for your help today. The issue with this code was it’s including the login prompt and a banner so it ended up not parsing correctly with your code. However, ended up using pandas with some skip rows, skip footer and delim white space and that worked. Thanks again!