You need these lines to indented within the for loop
net_connect = ConnectHandler(**device)
output = net_connect.send_command('show ip interface brief')
print(output)
output = net_connect.send_command('show version')
print(output)
Your code should look like this:
from getpass import getpass
from netmiko import ConnectHandler
password = getpass()
RTR_01 = {
"device_type": "cisco_ios",
"host": "10.10.10.10",
"username": "admin",
"password": password,
}
RTR_02 = {
"device_type": "cisco_ios",
"host": "10.10.10.11",
"username": "admin",
"password": password,
}
device_list = [RTR_01, RTR_02]
for device in device_list:
print("Connecting to the device :" + device["host"])
net_connect = ConnectHandler(**device)
output = net_connect.send_command("show ip interface brief")
print(output)
output = net_connect.send_command("show version")
print(output)
net_connect.disconnect() # to clear the vty line when done
And this is a better version of your code that does the same exact thing:
from getpass import getpass
from netmiko import ConnectHandler
password = getpass()
ipaddrs = ["10.10.10.10", "10.10.10.11"]
# A list comprehension
devices = [
{
"device_type": "cisco_ios",
"host": ip,
"username": "admin",
"password": password,
}
for ip in ipaddrs
]
for device in devices:
print(f'Connecting to the device: {device["host"]}')
with ConnectHandler(**device) as net_connect: # Using Context Manager
intf_brief = net_connect.send_command(
"show ip interface brief"
) # Inside the connection
facts = net_connect.send_command("show version") # Inside the connection
# Notice here I didn't call the `net_connect.disconnect()`
# because the `with` statement automatically disconnects the session.
# On this indentation level (4 spaces), the connection is terminated
print(intf_brief)
print(facts)
The outputs (intf_brief and facts) are printed outside the connection, because the session is not needed anymore to print any collected values.
forloop contain singleprintcall.