cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1531
Views
0
Helpful
2
Replies

Python For Looping

alfredobosca
Level 1
Level 1

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,

 

 

 

2 Replies 2

Jon Marshall
Hall of Fame
Hall of Fame

 

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

KRUNAL LATHIYA
Level 1
Level 1

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()