02-02-2020 11:15 AM
Hi folks,
After Cisco Live 2020 I decided to boost my learning about Python and I begun to write my first functions and brief code.
Now I tried to create a for looping for accessing all Cisco devices included in a list called: device_list but 'm getting next issue:
net_connect = Netmiko(**devices[i])
File "C:\Users\abosca\PycharmProjects\Python_Course\venv\lib\site-packages\netmiko\ssh_dispatcher.py", line 252, in ConnectHandler
return ConnectionClass(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'assword'
Code:
import os import datetime from netmiko import ConnectHandler from netmiko import Netmiko from dnac_resources import cisco_CSRV1, cisco_CSRV2 from dnac_functions import abosca_length #Create de LIST of devices devices = [cisco_CSRV1, cisco_CSRV2] for i in range (len(devices)): print("The number of Devices is:", len(devices)) net_connect = Netmiko(**devices[i]) print(devices[i]) #net_connect = Netmiko(**cisco_CSRV1) output = net_connect.send_command("show ip int brief") print(output)
By the way, If I'm not using for looping I can access correctly to cisco devices.
Many thanks,
02-02-2020 12:50 PM - edited 02-02-2020 01:11 PM
The error message is telling you the problem, you must have a typo in your device file ie. instead of "password" you have "assword" so check your file.
Also as a suggestion your for loop could be
for device in devices:
net_connect = ConnectHandler(**device)
output = net_connect.send_command("show ip int brief")
print(output)
unless you want to print out the same length each time through the loop ?
Jon
01-04-2024 04:39 AM - edited 01-04-2024 11:12 PM
Here's the revised version of your code with the proper for loop and import statement:
import datetime
from netmiko import Netmiko
from dnac_resources import cisco_CSRV1, cisco_CSRV2
# Create the list of devices
devices = [cisco_CSRV1, cisco_CSRV2]
# Print the number of devices (outside the loop)
print("The number of Devices is:", len(devices))
for device in devices:
try:
# Establish a connection to the device
net_connect = Netmiko(**device)
print(device)
# Send command and print the output
output = net_connect.send_command("show ip int brief")
print(output)
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Disconnect from the device
net_connect.disconnect()
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