Python

What is the difference between Paramiko and Netmiko?

When it comes to networking, there is a wide range of perspectives, and one cannot master how to interact with all the devices in the real world. However, all networking devices share similar functionality that, when mastered, are automatable.

As mentioned in my other tutorials, programmers are lazy and always looking to improve efficiency—thus doing the least work 🙂 —, and when it comes to automating network-related issues, many often jump at the chance.

In today’s quick guide, I’ll introduce you to automating SSH using two popular Python libraries: Paramiko and Netmiko. We will create simple python scripts using the two libraries to automate SSH and interact with network devices.

I choose this approach because a guide primarily focused on the differences between Paramiko and Netmiko would be too short—a simple table would suffice—and no-concrete. By taking this approach, you’ll be better able to experiment with them and see which does what and how.

Let’s get started:

What is Paramiko?

The official website defines Paramiko as follows:

“Paramiko is a Python (2.7, 3.4+) implementation of the SSHv2 protocol that provides both client and server functionality.”

I hope that was clear. Foundationally, it means Paramiko is a python library for interacting with SSH.

Now:

When we want to log in to a server, we know that we go to the terminal—cmd, xterm, MobaXterm, or just the Gnome Terminal—login, then execute commands.

Let’s see how Paramiko can help with that.

How to Install Paramiko

To use Paramiko, we need to install it. You should have Python – preferably python 2 – installed on your machine. Using pip, enter the command below to install Paramiko.

sudo pip install Paramiko
Python -m pip install paramiko

If you’re looking to build from the source, refer to the official Paramiko GitHub repository using the provided resource:

https://github.com/paramiko/paramiko

Connecting to SSH using Paramiko

To connect to SSH using Paramiko, we use the connect() method, which requires the hostname parameter—it also supports other parameters, but since we don’t require those, we can ignore them for now.

connect(hostname, port=22, username=None, password=None, pkey=None, key_filename=None, timeout=None, allow_agent=True, look_for_keys=True, compress=False, sock=None, gss_auth=False, gss_kex=False, gss_deleg_creds=True, gss_host=None, banner_timeout=None, auth_timeout=None, gss_trust_dns=True, passphrase=None, disabled_algorithms=None)

Using this function, which is available in paramiko. Client.SSHClient.connect(), connects to the specified hostname, and authenticates it. The target system gets checked against the existing local system keys (trusted).

If you have a specific host file, you can use the load_host_keys() method and set the Paramiko SSH client to add any unknown host to paramiko.AutoAddPolicy(). If you are using the client in untrusted systems, avoid using the paramiko.AutoAddPolicy.

Connecting SSH With A Password

From the connect method, we can see that we have the username and password parameters that we can use to connect to the system. Consider the code below to connect SSH via username and password.

from paramiko import util, SSHClient, AutoAddPolicy
client = SSHClient()
client.load_system_host_keys()
client.load_host_keys("/home/linuxhint/.ssh/known_hosts")
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(hostname="linuxhint.com", username="admin", password="AdminPassword")
client.close()

If you encounter issues when importing Paramiko SSHClient class, refer to the following stackoverflow question:

https://stackoverflow.com/questions/29378234/python-import-paramiko-error-cannot-import-name-util

Connecting SSH Via Key

As we all know, connecting SSH via key is more secure than using a raw password. Paramiko knows this and allows you to pass the key file and connect to the target system.

Consider the code below.

from paramiko import SSHClient
from paramiko AutoAddPolicy
client = SSHClient()
client.load_system_host_keys()
client.load_host_keys('/home/linuxhint/.ssh/known_hosts')
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect('linuxhint.com',username='admin',key_filename='ssh_key.pem', passphrase='AdminPassphrase')
client.close()

Running Commands Over SSH

Once you gain access to the system using SSH (over Paramiko), you can execute a series of commands. Consider the following code snippet:

client = SSHClient()
client.load_system_host_keys()
client.connect('linuxhint.com')
stdin, stdout, stderr = client.exec_command('bash”)
stdin.close()
stdout.close()
stderr.close()
client.close()

At this point, you know how to use the Paramiko library to automate SSH. As you can imagine, this is not a comprehensive Paramiko guide, and we do not dive into details related to what the tool does. The aim was to show you its implementation.

For more information, please refer to more comprehensive materials such as:

https://docs.paramiko.org/en/stable/
https://github.com/paramiko/paramiko

What is Netmiko?

Netmiko is very popular and similar to Paramiko with a few significant differences:

  • Device support
  • Performance

When working on real-world networks, you will come across various device models. Thus, you need a reliable tool that can help you automate the process. In some instances, you cannot use Paramiko due to device support limitations, leading to lags and crashes—you can check the supported devices on the official documentation. It is also considerably slower than Netmiko.

Paramiko is more of a generic SSH module that you can use to automate specific SSH tasks. In contrast, Netmiko is broader and well optimized for managing network devices such as switches and routers.

Abstraction is the other advantage of using Netmiko. Netmiko provides a simple function you can use to disable paging. For example, an output from the SSH session might be more than one page. Using regular SSH sessions, you will have to add an input-like space to show the next page. Netmiko provides you with a way to override this.

Advantages of Netmiko over Paramiko are:

  • Automatically connect via SSH to network devices.
  • It provides simpler execution of shows commands and data output.
  • Provides simpler functionality for configuration commands including commit actions.
  • Multi-device support across network device vendors and platforms.

How to Install Netmiko

Installing Netmiko is also relatively easy:

All you have to do is ensure you have Python and pip installed on your system and execute the command:

pip install netmiko
python -m pip install netmiko

Connecting to SSH using Netmiko

Connecting to device SSH sessions using Netmiko is very simple. Remember that Netmiko is more optimized for devices such as routers and not generic SSH.

Consider the code snippet below.

#import modules
from netmiko import ConnectHandler
# device info in dictionary format.
device_config = {
 â€śdevice_type”: “cisco_ios”,
 â€śip”: “192.168.0.1”,
 â€śusername”: “admin”,
 â€śpassword”: “password”,
 â€śsecret”: “password”
}
connection = ConnectHandler(**device_config)

Using the simple code above, you will have an SSH connection to the device. You can also pass the device information directly instead of passing it to a dictionary.

Once you have an SSH session, you can execute commands using the send_command() function. Functions supported by netmiko include:

Netmiko commonly-used methods:

  • net_connect.send_command() – This functions sends commands down a network channel and return output based on pattern.
  • net_connect.send_command_timing() – Returns output based on timing from a command sent down a network channel.
  • net_connect.send_config_set() – Applies configuration settings to remote devices.
  • net_connect.send_config_from_file() – Applies configuration settings from an external file
  • net_connect.save_config() – Exports and saves the running configuration as the startup configuration.
  • net_connect.enable() – Queries the device to activate enable mode.
  • net_connect.find_prompt() – Return the current router prompt
  • net_connect.commit() – Runs commit commands on devices such as Juniper and IOS-XR
  • net_connect.disconnect() – Terminates the sessions
  • net_connect.write_channel() – Enables low level write
  • net_connect.read_channel() – Enables low level read.

As mentioned earlier, this is not a guide on how to use Netmiko but a simple direction of what Netmiko and Paramiko are. Check the official documentation for more information.

https://github.com/ktbyers/netmiko

Conclusion

In this quick guide, we discussed how to use paramiko for generic SSH connections and Netmiko network device management, illustrating the differences between the two.

To conclude:

Paramiko Netmiko
Useful for generic ssh use Most useful for network device configuration.
Limited support for a wide range of network devices. Supports a wide range of network devices.

About the author

John Otieno

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list