Reputation: 53
I am new netmiko library but the documentation seems to be fairly straight forward, not sure what I am missing in my code (or a bug i am not sure) that is not letting the commit go through on Cisco ios.
device = ConnectHandler(device_type="cisco_ios",
host="cisco-lab-device",
username="admin",
password="passwd",
session_timeout=120,
verbose=True)
#session_log="output.txt")
cmdlist = ['vrf new']
try:
result = device.send_config_set(cmdlist,exit_config_mode=False)
print("Result after sending commands ", result)
except Exception as err:
device.disconnect()
return
result = device.commit()
print('DONE') # I dont see this print at all
device.disconnect()
##This is the dump upon running the code:
Result after sending commands
RP/0/RP0/CPU0:cisco-lab-device#
RP/0/RP0/CPU0:cisco-lab-device#
RP/0/RP0/CPU0:cisco-lab-device#configure terminal
RP/0/RP0/CPU0:cisco-lab-device(config)#vrf new
RP/0/RP0/CPU0:cisco-lab-device(config-vrf)#
As I mentioned above, i dont see the print trace that i added after commit method. And also I checked show running-config on cisco box looks like the commit didnot go through and new vrf creation is not reflected. I tried with bunch of other commands as well its stuck at the same point. Can someone please shed some light what I could be missing here??
Upvotes: 0
Views: 875
Reputation: 71
Per the comment from Tes3awy your device is running Cisco IOS XR, vs vanilla Cisco IOS. The latter has no concept of a commit operation so device.commit() is not a valid operation.
So you need to change:
device_type="cisco_ios"
To:
device_type="cisco_xr"
Additional info: Cisco IOS has no concept of a commit operation - commands take effect immediately when entered. Hence, no commit() function needs to be implemented.
Upvotes: 0
Reputation: 105
This config could help you :
from netmiko import ConnectHandler
RTR_150 = {
"device_type": "cisco_ios",
"host": "router01",
"username": "test",
"password": "test",
"secret": "secret_if_you_need"}
with ConnectHandler(**RTR_150) as net_connect:
config_commands = [ "int loo 0",
"ip add 1.1.1.1 255.255.255.0",
"no shut",
"do show ip int brief"]
net_connect.enable()
output = net_connect.send_config_set(config_commands)
net_connect.disconnect()
print(output)
Upvotes: 0
Reputation: 466
Try to build on this code.
It is simple code that will take device ip, username, password and device-type. It will connect to device and send commands you type in config_commands list. It will send commands and print you the output. And you can sand single command, just put it in send_command("example_command")
from netmiko import ConnectHandler
from getpass import getpass
password = getpass()
RTR_150 = {
'ip': '150.154.1.10',
'username': 'test',
'password': 'test',
'device_type': 'cisco_ios',
}
net_connect = ConnectHandler(**RTR_150)
config_commands = [ 'int lo0',
'ip add 1.1.1.1 255.255.255.0',
'no shut' ]
output = net_connect.send_config_set(config_commands)
print(output)
output = net_connect.send_command('show ip int brief')
print(output)
Upvotes: 1