loop through a nested dictionary to connect in multiple vendor devices in Napalm

222 Views Asked by At

I have NAPALM install and working to get facts from a single device at a time. However i deviced to make things a bit different, now I am trying to use a nested dictionary to connect to my devices, but I am getting a error.

import napalm
from napalm_ros import ros


database_devices={
    "cisco":{
    "hostname":"152.0.0.2",
    "type":"ios",
    "username":"cisco",
    "password":"cisco",
    "optional_args":{"secret" : "cisco"}
},
"mikrotik":{
    "hostname":"152.0.0.2",
    "type":"ros",
    "username":"mikrotik",
    "password":"mikrotik",
    "optional_args":{"port" :  "8728 "}
}
}


for key,values in database_devices.items():
    print("Connecting to " + key)
    driver=napalm.get_network_driver(values.get("type",{}))

    device=driver(
    hostname=values.get("hostname",{})
    username=values.get("username",{})
    password=values.get("password"),{})
    optional_args=values.get("optional_args",{})


    device.open()

I tried to put comma between my devices in the device=driver , but I cant connect either. And I think I will have another problem when I can fix this, my optional_args value is different in cisco and mikrotik, in cisco, my optional arg is my enable password, in mikrotik is a port.How I could get this values into my for ? thanks for any help.

2

There are 2 best solutions below

1
On

Can you please try this as your data is in nested dictionary for key,values in database_devices.items(): values[1].get('hostname',{})

0
On

Please try this it is working for me. Let me know nested dictionary

for k,v in database_devices.items():
      print('hostname '+v['hostname'])
      print('type:',v['type'])
      print('username: ',v['username'])
      print('password: ',v['password'])
      print('optional_args ',v['optional_args'])