SSH Connection to Multiple Devices with Netmiko

Tolga Koca
2 min readMay 8, 2023

--

In this article, we make SSH connections to multiple network devices with the netmiko module. In this article here, we connect to a single device. You can also check the paramiko script to login to devices here. We use the netmiko module with the for loop to login devices one by one.

We import the “Netmiko” function from the netmiko module. Then, we define 3 variables as host1, host2, host3, and we add them to a devices list. We define the device information. It’s a dictionary of data types that netmiko can understand. We have the host as the IP address, username, password, and device_type to define the device brand/model/type. Here, the device type is “cisco_ios”. You can check the full list from its documentation. These are the mandatory keys to describe a device. We also have global_delay_factor, which adds a time delay between running the commands on the remote device. We define it as 0.1 seconds.

from netmiko import Netmiko

host1 = {
"host": "10.1.1.1",
"username": "admin",
"password": "test",
"device_type": "cisco_ios",
"global_delay_factor": 0.1
}

host2 = {
"host": "10.1.1.2",
"username": "admin",
"password": "test",
"device_type": "cisco_ios",
"global_delay_factor": 0.1
}

host3 = {
"host": "10.1.1.3",
"username": "admin",
"password": "test",
"device_type": "cisco_ios",
"global_delay_factor": 0.1
}

devices = [host1, host2, host3]

We use the for loop to login to each device one by one. In each iteration, the code gets host1, host2, and host3 orderly and makes the connection. We make the device connection with the “Netmiko(**host)” code which is simpler than paramiko.

Finally, we print the output of the configuration and show commands with the print function. You can check the detailed explanation of those commands in this article.

for host in devices:
net_connect = Netmiko(**host)
config = ["interface gigabitethernet 1/2", "description new_port"]
command = "show version"
config_output = net_connect.send_config_set(config)
show_output = net_connect.send_command(command)
net_connect.disconnect()
print(config_output)
print(show_output)

You can enroll in my “Network Automation with Python” — LIVE training HERE.

For +100 Python networking scripts, check my book HERE.

Here is the full code:

--

--