SSH Connection to a Device with Paramiko

Tolga Koca
3 min readMay 5, 2023

--

We can connect network and system devices with SSH protocol. In Python, the most popular connection modules are paramiko and netmiko. In this article, we use the paramiko module to login to a single device and send a “show” command to a Cisco Router. You can modify this code to login to any device with an SSH connection. To use netmiko, you can check the article here.

To login to multiple devices, you can check the following article here.

Because the “paramiko” is a 3rd party module, we must install the paramiko module to the project with the “pip install paramiko” command in the terminal. Or you can add it with GUI if you use an IDE tool such as Pycharm.

Then we can import the paramiko module with the “import paramiko”. We can also add the built-in time module with the “import time”. It is often used to add delays to the code.

import paramiko
import time

There are some functions to run in paramiko to login to the devices.
First, we call the “SSHClient” function for SSH connection and bind it to the “client” variable.
Then, we call “AutoAddPolicy” inside the “set_missing_host_key_policy” function in the “client” variable. When we connect to a network/system device for the first time, it displays a message to trust or not the remote host. This is an SSH protocol security step. Once we click on the option to trust this device, it will never ask that question again, and this function automatically passes it.

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

After that, we call the “connect” function in the “client” variable, which generates the connection information such as IP address, port, username, and password in order.

client.connect("10.10.10.1", 22, "admin", "cisco")

With the “invoke_shell” function, we request an interactive shell session on the channel and bind it to the commands variable.

commands = client.invoke_shell()

All of the above commands are used for each paramiko connection. So you can copy and paste it into other codes. There is no need to check what happens in each line deeply; the basic information above should be enough.

Now, we can call the “send” function to send a command to the remote host. After we write the command, we must send enter command to execute the commands. So we write “\n” as enter in the Python programming. As we run this command in the Cisco router, we should get the output of the “show version” command. You can do this process for any device with an SSH connection.

commands.send("show version \n")

Optionally, we can add a “sleep” function from the time module. Because the command output can be large, displaying all output can take some time. So, we add 3 seconds to suspend the code to get all output of the “show version” command.

time.sleep(3)

Finally, we can get the output from the device. We use 2 functions, “recv” and “decode”. In “recv”, we write an integer inside the parentheses to the max value that output is shown. If we write 5, for example, the code only displays the 1st 5 characters of the output. So, you can make this number big for large outputs. In the “decode” function, we convert the output to the “utf-8” format, which is human-readable. After that, we print the output variable.

output = commands.recv(1000000)
output = output.decode("utf-8")
print (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:

--

--