Search code examples
pythonpython-3.xnetmiko

Filtering Python Output


I'm new to Python programming, so please bear with me. I'm a network engineer and I've been toying around with Netmiko to pull some information from our routers and switches. I've run the following code to pull interface descriptions from our boxes:

from netmiko import ConnectHandler

device = ConnectHandler(device_type='cisco_ios', ip='1.2.3.4', username='user', password='password')
output = device.send_command("show run | i description")
print (output)
device.disconnect()

This worked well to get what I needed, but what I'm trying to do is filter the output. Within the interface descriptions, we have circuit ID's of our customer circuits they pertain to. For example, one interface description might read like this:

description Customer/ A56I0

All of our circuit ID's looks something like that, and what I'm trying to do is filter the printed out put to only include those and not anything else. To clarify, if the whole line on the interface reads "description Customer/ A56I0", I would like my output to only read "A56I0". How would I accomplish this?

****EDIT****

He's an example of what the above script outputs:

description Customer/Order A79PD
description Customer/Order A79PF
description Customer/Order AA6VG
description Customer/Order A79PE
description Customer/Order A79PC
description Customer/Order AA6VV
description Customer/Order A79PJ
description Customer/Order A79PB
description Customer/Order AA6VA

What I'm trying to do is get only those last 5 characters for each line it's pulling so it looks like this:

A79PD
A79PF
AA6VG
A79PE
A79PC
AA6VV
A79PJ
A79PB
AA6VA

Solution

  • if the last 5 characters are the ID then, It can be accomplished by the following code

    from netmiko import ConnectHandler
    
    device = ConnectHandler(device_type='cisco_ios', ip='1.2.3.4', username='user', password='password')
    output = device.send_command("show run | i description")
    #Change here
    for i in output.splitlines():
        print (i[-5:])
    device.disconnect()