cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
396
Views
15
Helpful
5
Replies
Highlighted
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 ~]$

 

Everyone's tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Rising star

Re: Python netmiko interactive session on Cisco IOS

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

5 REPLIES 5
Highlighted
Rising star

Re: Python netmiko interactive session on Cisco IOS

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

Highlighted
Beginner

Re: Python netmiko interactive session on Cisco IOS

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

Highlighted
Rising star
Rising star

Re: Python netmiko interactive session on Cisco IOS

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

Highlighted
Beginner

Re: Python netmiko interactive session on Cisco IOS

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.

Highlighted
Rising star
Rising star

Re: Python netmiko interactive session on Cisco IOS

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