My original question was answered but I am now facing another issue.
Get index name of a list made from dictionaries
I was trying to get the name of a dictionary from a list and use it in a for
loop but it seems that it can't be done. Nested dictionaries was the solution to my issue.
Now I can successfully send command to network devices using Netmiko but it seems it only uses the last entry in the dictionary. Here's the code:
from netmiko import ConnectHandler
site1_switches = {
'visw0102' : {
'device_type': 'hp_comware',
'ip': '192.168.0.241',
'username': 'admin',
'password': 'password'
},
'visw0103' : {
'device_type': 'hp_comware',
'ip': '192.168.0.242',
'username': 'admin',
'password': 'password'
},
'visw0105' : {
'device_type': 'hp_comware',
'ip': '192.168.0.244',
'username': 'admin',
'password': 'password'
}
}
vlans = {
'300': 'TEST1',
'310': 'TEST2',
'320': 'TEST3',
'330': 'TEST4',
'340': 'TEST5'
}
for key, values in site1_switches.items():
device_type = values.get('device_type', {})
ip_address = values.get('ip', {})
username = values.get('username', {})
password = values.get('password', {})
net_connect = ConnectHandler(device_type=device_type, host=ip_address, username=username, password=password)
output = net_connect.send_command_timing(
'y',
strip_prompt=False,
strip_command=False
)
output = net_connect.send_command_timing(
'_cmdline-mode on',
strip_prompt=False,
strip_command=False
)
print (output)
if 'Continue' in output:
output += net_connect.send_command_timing(
'y',
strip_prompt=False,
strip_command=False
)
print (output)
if 'ssword' in output:
net_connect.send_command_timing(
'512900',
strip_prompt=False,
strip_command=False
)
print (output)
output = net_connect.send_command_timing(
'system-view',
strip_prompt=False,
strip_command=False
)
print (output)
for tag, vlan_name in vlans.items():
output = net_connect.send_command_timing(
'vlan' + ' ' + tag,
strip_prompt=False,
strip_command=False
)
print (output)
output = net_connect.send_command_timing(
'description' + ' ' + vlan_name,
strip_prompt=False,
strip_command=False
)
print (output)
The commands are ran successfully but only for the last entry in the nested dictionary (VISW0105). Here's the output:
administrator@vimgmt0103:~$ python3 test_netmiko.py
_cmdline-mode on
All commands can be displayed and executed. Continue? [Y/N]
_cmdline-mode on
All commands can be displayed and executed. Continue? [Y/N]y
Please input password:
_cmdline-mode on
All commands can be displayed and executed. Continue? [Y/N]y
Please input password:
system-view
System View: return to User View with Ctrl+Z.
[VISW0105] <-- This is the last entry (switch) in the dictionary
vlan 300
[VISW0105-vlan300]
description TEST1
[VISW0105-vlan300]
vlan 310
[VISW0105-vlan310]
description TEST2
[VISW0105-vlan310]
vlan 320
[VISW0105-vlan320]
description TEST3
[VISW0105-vlan320]
vlan 330
[VISW0105-vlan330]
description TEST4
[VISW0105-vlan330]
vlan 340
[VISW0105-vlan340]
description TEST5
[VISW0105-vlan340]
administrator@vimgmt0103:~$
I am trying to figure out why it skips over the other entries. Any idea?
Thanks!