How to install and use paramiko for SSH connections in Python

SSH (Secure Shell) is a protocol that allows you to securely access remote machines over a network. It can be used for various purposes, such as executing commands, transferring files, or tunneling network traffic. We will learn how to install and use Paramiko, a Python library that provides an easy interface for SSH connections.

Paramiko is a pure Python implementation of the SSHv2 protocol. It supports authentication, encryption, compression, and interactive sessions. It also provides higher-level classes and methods for common SSH tasks, such as SFTP (Secure File Transfer Protocol) and SSH port forwarding.

Step 1: Installing Paramiko

To install Paramiko, you can use pip, the Python package manager. Open a terminal and run the following command:

pip install paramiko
    

Alternatively, you can download the source code from GitHub and install it manually:

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

Step 2: Importing Paramiko

To use Paramiko, you need to import it in your Python script. For example:

import paramiko
    

Step 3: Establishing an SSH Connection

To establish an SSH connection, you need to create an SSHClient object and call its connect method with the hostname, username, and password of the remote machine. For example:

ssh = paramiko.SSHClient()
ssh.connect('example.com', username='user', password='pass')
    

You can also use a private key file instead of a password for authentication. For example:

ssh = paramiko.SSHClient()
ssh.connect('example.com', username='user', key_filename='id_rsa')
    

By default, Paramiko does not verify the host key of the remote machine, which can pose a security risk. To enable host key verification, you need to load the known_hosts file and set the missing_host_key_policy to RejectPolicy. For example:

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.RejectPolicy)
ssh.connect('example.com', username='user', password='pass')
    

Step 4: Executing Commands

Once you have established an SSH connection, you can execute commands on the remote machine using the exec_command method. This method returns three file-like objects: stdin, stdout, and stderr. You can write to stdin to provide input to the command, and read from stdout and stderr to get the output and error messages. For example:

stdin, stdout, stderr = ssh.exec_command('ls -l')
print(stdout.read().decode())
print(stderr.read().decode())
    

Step 5: Transferring Files

To transfer files between the local and remote machines, you can use the SFTPClient class. You can get an SFTPClient object from an SSHClient object using the open_sftp method. For example:

sftp = ssh.open_sftp()
    

The SFTPClient class provides methods for common file operations, such as get, put, listdir, stat, remove, rename, etc. For example:

sftp.get('remote_file.txt', 'local_file.txt')
sftp.put('local_file.txt', 'remote_file.txt')
sftp.listdir('.')
sftp.stat('remote_file.txt')
sftp.remove('remote_file.txt')
sftp.rename('remote_file.txt', 'remote_file.bak')
    

Step 6: Closing the SSH Connection

To close an SSH connection, you can call the close method of the SSHClient or SFTPClient object. For example:

ssh.close()
sftp.close()
    

Paramiko is a powerful and easy-to-use library for SSH connections in Python. It can help you automate various tasks on remote machines without hassle.

See also  How to Execute Remote Commands with Paramiko and SSHClient