4

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

2 Answers2

1

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()
sai Pavan Kumar
  • 1,129
  • 12
  • 20
  • This did what I wanted, but only gave me just one circuit ID instead of all of them on the switch. What needs to be added to allow it to keep running until it pulls them all? – DragonFist616 Nov 01 '19 at 16:28
  • What appears to be happening is that its only giving me the last 5 characters of the entire output, and not the last 5 characters of each line in each interface description like I'm trying to get. – DragonFist616 Nov 01 '19 at 16:39
  • if you give me more than 2 interface description lines I can write the code.I am not understanding a word you’re saying,so please give me the input – sai Pavan Kumar Nov 01 '19 at 16:41
  • I apologize for the confusion. I have edited the original question to clarify what I'm looking for – DragonFist616 Nov 01 '19 at 16:46
1

Use splitlines() on the output. You'll get a list, loop over it and print(line[-5:])

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")

for i in output.splitlines(): 
    print(i.split()[-1])
device.disconnect()
sai Pavan Kumar
  • 1,129
  • 12
  • 20
user6399774
  • 116
  • 6