03-30-2020 02:13 AM - edited 03-30-2020 02:14 AM
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 ~]$
Solved! Go to Solution.
03-30-2020 04:21 AM - edited 03-30-2020 10:17 PM
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
03-30-2020 04:21 AM - edited 03-30-2020 10:17 PM
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
03-31-2020 11:57 PM
It's simpler that I thought .. many thanks man. I can't believe simple while loop can do this.
08-04-2020 11:11 PM
I don't think that is a genuine way......
it is disconnecting session rather than actually sending exit command.
03-30-2020 06:26 AM - edited 03-30-2020 06:27 AM
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
03-31-2020 11:59 PM
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.
04-01-2020 01:06 AM
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
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide