What is Paramiko?
Paramiko is a Python library for Secure Shell (SSH) protocol implementation, which enables Python applications to access, configure, and manage remote servers securely. Paramiko is not part of Python’s standard library.
It is primarily used for automating system administration tasks, transferring files securely, and establishing secure connections to remote systems. When your Python program needs to run an external password-dependent program, or access a remote server, you can use Paramiko.
Paramiko provides both client and server functionality for SSH protocol, allowing developers to build SSH-based applications in Python. Some of the features provided by Paramiko include:
- SSH client and server implementation
- Support for SFTP (Secure File Transfer Protocol)
- Support for SCP (Secure Copy Protocol)
- Support for SSH keys and password authentication
- Support for port forwarding
Learn Python Basics
Installing Paramiko
To use Paramiko in your Python program, you have to first install it. You can install Paramiko in your Python environment using pip. See the code below:
pip install paramiko
To install Paramiko in the anaconda environment, you can use the below code:
conda install -c anaconda paramiko
A simple working example of Paramiko
Connecting to the server using the password
Here’s an example of how to use Paramiko to establish an SSH connection to a remote server using the password:
import paramiko
# create SSH client object
client = paramiko.SSHClient()
# automatically add the server's SSH key
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# connect to the remote server
client.connect('hostname', username='username', password='password')
# run a command on the remote server
stdin, stdout, stderr = client.exec_command('ls')
# print the output of the command
print(stdout.read().decode())
# close the connection
client.close()
In the above code, we first create an SSH client object and set the missing host key policy to automatically add the server’s SSH key. We then use the connect() method to establish an SSH connection to the remote server, passing in the hostname, username, and password.
Once the connection is established, we use the exec_command() method to run a command on the remote server, in this case ls. We then print the output of the command using the stdout object returned by the exec_command() method. Finally, we close the connection using the close() method on the SSH client object.
Paramiko also provides other methods and features for working with SSH connections, such as SFTP file transfers, port forwarding, and SSH key authentication.
Connecting to the server using SSH key
Here’s an example of how to use Paramiko to establish an SSH connection to a remote server using the SSH key:
import paramiko
def examine_last(server, connection):
command = "sudo last"
expected = ["user1", "reboot", "root", "sys-admin"]
_stdin, stdout, _stderr = connection.exec_command("sudo last")
lines = stdout.read().decode()
connection.close()
for line in lines.split("\n"):
# Ignore the last line of the last report.
if line.startswith("wtmp begins"):
break
parts = line.split()
if parts:
account = parts[0]
if not account in EXPECTED:
print(f"Entry '{line}' is a surprise on {server}.")
def key_based_connect(server):
host = "192.0.2.0"
special_account = "user1"
pkey = paramiko.RSAKey.from_private_key_file("./id_rsa")
client = paramiko.SSHClient()
policy = paramiko.AutoAddPolicy()
client.set_missing_host_key_policy(policy)
client.connect(host, username=special_account, pkey=pkey)
return client
def main():
server_list = ["worker1", "worker2", "worker3"]
for server in server_list:
connection = key_based_connect(server)
examine_last(server, connection)
main()