What is the simplest way to SSH using Python?



SSH is referred as secure shell which is useful to remotely manage computers in a secure manner. To connect to a server, we typically use PuTTy, MobaXTerm or the command-line ssh application. Every Unix, Linux and Mac server includes SSH as standard equipment and it is usable in every data centre.

SSH connections have made it feasible for secure remote access to resources, remote command execution, the transmission of software patches and updates and other administrative or management tasks.

SSH is used in systems administration and file transfer software as well as to handle routers, server hardware, virtualization platforms and operating systems (OSes). Additionally, it creates a secure link between nearby and distant computers.

SSH using Paramiko

Python provides several libraries for SSH, but the simplest and most popular way is to use paramiko library. We can install paramiko is using pip -

python -m pip install paramiko

To check if paramiko is installed in our working environment, we need to execute the following command -

pip list

Upon checking, the following details will be displayed.

packaging                20.9
pandas                   1.3.5
pandocfilters            1.4.3
paramiko                 2.11.0
parso                    0.82
path                     16.4.0 

Install paramiko using .whl file offline.

The other way to install paramiko library is by using the .whl file, which we can get from the link https://pypi.org/project/paramiko/#files . After downloading the files, we need to install them by using the below command -

pip install paramiko-2.7.2-py2.py3-none-any.whl

We can also clone from GitHub and use setup.py to install directly from the source code with the help of below command -

git clone https://github.com/paramiko/paramiko
cd paramiko
python setup.py install

Using Paramiko

To use paramiko, we should make sure that we have correctly set up SSH in our system.

To set up the SSH, the keys need to be downloaded from the site https://confluence.atlassian.com/bitbucketserver/creating-ssh-keys-776639788.html on the host machine, and when running the python script then these keys are accessible. Once that is done, use the following program to connect to a remote server using ssh.

from paramiko import SSHClient
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('user@server:path')
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls')
print(ssh_stdout) #print the output of ls command

Use the paramiko.client to connect to an SSH server. SSHClient.connect(). The only mandatory parameter is the hostname.

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)
Updated on: 2025-06-07T22:28:13+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements