cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3672
Views
15
Helpful
6
Replies
fc00::/7
Beginner

Python netmiko interactive session on Cisco IOS

This is simple Python code to get `show hostname` output in Cisco IOS.

script.py

from netmiko import ConnectHandler

cisco_881 = {
'device_type': 'cisco_ios',
'host': 'host',
'username': 'u',
'password': 'p'
}

net_connect = ConnectHandler(**cisco_881)

output = net_connect.send_command('show hostname')
print(output)

Output

[user@Linux ~]$ python script.py
Hostname is Cisco_01

[user@Linux ~]$

Instead of defining the Cisco command in the code, would it be possible to get interactive mode so that I can type anything on the Cisco IOS Shell?

 

Desired Ouput

[user@Linux ~]$ python script.py

Cisco_01 #
Cisco_01 # show hostname
Hostname is Cisco_01
Cisco_01 # exit
Connection to Cisco_01 closed.

[user@Linux ~]$

 

1 ACCEPTED SOLUTION

Accepted Solutions
Sergiu.Daniluk
VIP Engager

Hi,

Try this: 

from netmiko import ConnectHandler

device = {
'device_type': 'cisco_nxos',
'host': 'ip',
'username': 'u',
'password': 'p'
}

net_connect = ConnectHandler(**device)
while True:
command = input("Router# ")
if command == "exit":
net_connect.disconnect()
print("Disconnected!")
break
output = net_connect.send_command(command)
print(output)

 Result:

Router# show hostname
N9K-1

Router# show version
Cisco Nexus Operating System (NX-OS) Software
...
Router# exit
Disconnected!

Regards,

Sergiu

View solution in original post

6 REPLIES 6
Sergiu.Daniluk
VIP Engager

Hi,

Try this: 

from netmiko import ConnectHandler

device = {
'device_type': 'cisco_nxos',
'host': 'ip',
'username': 'u',
'password': 'p'
}

net_connect = ConnectHandler(**device)
while True:
command = input("Router# ")
if command == "exit":
net_connect.disconnect()
print("Disconnected!")
break
output = net_connect.send_command(command)
print(output)

 Result:

Router# show hostname
N9K-1

Router# show version
Cisco Nexus Operating System (NX-OS) Software
...
Router# exit
Disconnected!

Regards,

Sergiu

View solution in original post

It's simpler that I thought .. many thanks man. I can't believe simple while loop can do this.

I don't think that is a genuine way...... 

 

it is disconnecting session rather than actually sending exit command.

omz
VIP Collaborator VIP Collaborator
VIP Collaborator

Although there are better ways to write this code .. you can try this ..

 

#!/usr/bin/env python
from netmiko import ConnectHandler

cisco_881 = {
'device_type': 'cisco_ios',
'host': '172.16.100.17',
'username': 'cisco',
'password': 'cisco123'
}

net_connect = ConnectHandler(**cisco_881)
output = net_connect.send_command('',expect_string=r'#',strip_command=False, strip_prompt=False)
output += net_connect.send_command('\n',expect_string=r'#',strip_command=False, strip_prompt=False)
output += net_connect.send_command('show run | i hostname',expect_string=r'#',strip_command=False, strip_prompt=False)
net_connect.disconnect()
print(output)

Your desired output .. does it have to match? Instead of exit command you can use net_connect.disconnect() to close the session.

omz@mac ios % python script.py

Switch#
Switch#show run | i hostname
hostname Switch
Switch#
omz@mac ios %

HTH

Thanks but this is not what I looking for.


In this code, Cisco command needs to be defined.

'show run | i hostname'

What I wanted to achieve is to run arbitrary command in Cisco devices as shown in msdaniluk's code.

omz
VIP Collaborator VIP Collaborator
VIP Collaborator

ahh  .. I didn't read the requirement properly .. my bad :)

Instead of defining the Cisco command in the code, would it be possible to get interactive mode so that I can type anything on the Cisco IOS Shell?

glad you got the answer

Content for Community-Ad