Python

Python Paramiko Example

Paramiko is a well-known python library widely used by developers to create SSH Networks jointly, i.e., client and server. You can say that the Paramiko package is the employment of protocol SSHv2. You can call Paramiko an untainted edge for Python for the implementation of SSH networking functionality. On the other side, it also provides little usage of the C extension of a python to do some low-level cryptography. So, within this article today, we will be discussing the role of Paramiko in making client-server connections and see implementation as well. Thus, let’s get started.

Usage of SSHv2:

SSH is the functionality of making client and server connections. It is extensively used to contact remote servers, perform some instructions, and upload or download documents from the servers. For file transfers, the SFTP and SCP protocols are known to be used in the scripts with SSHv2. The protocol SSHv2 has been used in the Paramiko for security reasons among python codes as the alternative to SSL. Due to the usage of SSHv2, developers can implement all well-known hash functions and ciphers in codes.

Update System:

Start by logging in from your Linux system. In our case, we have been using the Ubuntu 20.04 Linux system. All the work should be done using the command line. Hence, we opened the terminal shell by Activity area held at the top of the Ubuntu 20.04 desktop. Tap on the “Activity” menu and write the name of an application on the search bar. Tap the enter button to view the results. After searching, you will get the application as a pop-up. Tap on it simply to launch. Another way to open it is via the shortcut key, i.e., Ctrl+Alt+T. The terminal is launched already; we have to make sure that our system is up to date and ready. Use the below apt instruction to make it up to date.

$ sudo apt update && sudo apt upgrade

Install Python:

To use paramiko, the system must have python’s latest version installed and configured on it. You can verify the installed version of python on your Linux system using the version command below.

$ python –version

If you don’t find any Python version installed on your system, then install it first. It requires the prerequisite of the “pip” repository as well. So, make sure to install “pip” as well. You have to use the below two stated commands in a shell with sequence to do so. It may require the sudo rights to install python3. Add your root account password, and you are good to go.

$ sudo apt install python3-pip
$ sudo apt install python3

Install Paramiko:

To see the functionality of Paramiko, you have first to install it on your system. For this purpose, we have to utilize the installed “pip” package on the shell within the installation command. Thus, we have been using sudo rights in our command to install paramiko library/package in our Linux environment within python with the “pip” package. The command for the installation of the paramiko package has been stated below. After pressing the “Enter” button to execute these commands, it requires a sudo password to continue. We have provided the password and hit the key “Enter”. It shows that our Python3 version is already satisfying the requirement of the paramiko package in the image.

$ sudo pip install paramiko

Enable SSH:

To use paramiko, you have to ensure that the SSH protocol is enabled in your Linux system. After the installation, makes sure to check its status. If it’s not active, try to enable it with the systemctl command.

$ sudo apt install openssh-server
$ sudo systemctl start ssh
$ sudo systemctl enable ssh
$ sudo systemctl status ssh

Now, you can check that the SSH protocol has been working fine on your system as per the command shown in the attached image below.

$ ssh

Paramiko Example:

Let’s begin with the implementation of a paramiko example. To start it, you have to just create a file with the “.py” python extension. Creating the file with an extension is necessary because the file won’t work without it in the python environment. So, use the terminal shell once again to create a file with the “touch” keyword. The command is stated as follows:

$ touch par.py

Your file must have been created in the home folder of your Linux system. You can open it in any editor to start coding, i.e., text editor, vim, GNU nano editor. To do coding, we recommend you open it in the text editor, write your code, save and then run it on the shell. But, we have preferred to code in the GNU Nano editor while opening it via the “nano” keyword on the shell. The command for opening this file has been stated below.

$ nano par.py

Now the file is successfully launched in the GNU Nano editor. Add the code shown below in your file as it is and save it using the shortcut key “Ctrl+S” from the keyboard.

Let’s start explaining this code. The first line shows that the paramiko package or API has been imported into the code to utilize it further. You can also add the python-support at the top of this file, i.e., “#!/usr/bin/python”. You have to understand that the machine on which we are currently working is our client right now. To connect this client with some server host, we also need to mention the host variables or information in the code. Therefore, we have also mentioned the host information in our code.

We have been connecting our client machine to the host server “test.rebex.net”. So, we have created a variable “host” and assigned it a host URL. We have to mention the port number via the variable “port” to whom our host and client will connect. The username and password for your host must be given in the variables. The variable “command” will be listing all files and folders held on our server host. So, we have created a client object “ssh” of “SSHClient” with the paramiko package. This object calls the automatic policy function of adding unknown keys to perform SSH to remote host servers via the paramiko package. The same object is used to connect the client machine with the host server via the host credentials.

The variable “command” has been passed to the “exec_command()” function to get a 3-tuple result. The result has been outputted via the readlines() function.

import paramiko
host = "test.rebex.net"
port = 22
username = "demo"
password = "password"
command = "ls"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)
stdin, stdout, stderr = ssh.exec_command(command)
lines = stdout.readlines()
print(lines)

Upon the execution of a file, we got the results as below.

$ python3 par.py

Conclusion:

We have discussed a step-by-step flow of concepts for the implementation of Python Paramiko. It goes from the introduction and installation of paramiko to understanding and enabling SSH protocol. This article will help the users to install pip, python and update the Linux system once at all. In the end, we have also briefly discussed a simple example of implementing paramiko in python to get a clear understanding. To sum up, this article will be a bonus to Linux users who are unfamiliar with paramiko and SSH concepts.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.