[Awsome Python] Paramiko: A pure-Python implementation of the SSHv2 protocol

Paramiko

Paramiko is a pure-Python (2.7, 3.4+) implementation of the SSHv2 protocol, providing both client and server functionality. It provides the foundation for the high-level SSH library Fabric, which is what we recommend you use for common client use-cases such as running remote shell commands or transferring files.

Direct use of Paramiko itself is only intended for users who need advanced/low-level primitives or want to run an in-Python sshd.

Installation

The recommended way to get Paramiko is to install the latest stable release via pip:

1
$ pip install paramiko

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import paramiko

# Create a SSH Connetion
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='hostname', username='username', password='password')

# Run commands on the remote host
stdin, stdout, stderr = ssh_client.exec_command(“ls”)

print(stdout.readlines())
# [u’anaconda-ks.cfg\n’, u’database_backup\n’, u’Desktop\n’, u’Documents\n’, u’Downloads\n’, …. u’Public\n’, u’Templates\n’, u’Videos\n’]

print(stderr.readlines)
# []

# Downloading a file from remote machine
ftp_client = ssh_client.open_sftp()
ftp_client.get(‘remotefileth’,’localfilepath’)

# Uploading file from local to remote machine
ftp_client.put(‘localfilepath’,remotefilepath’)

# Close connection
ftp_client.close()

References

[1] paramiko/paramiko: The leading native Python SSHv2 protocol library. - https://github.com/paramiko/paramiko

[2] Module paramiko — Python for network engineers 1.0 documentation - https://pyneng.readthedocs.io/en/latest/book/18_ssh_telnet/paramiko.html

[3] How to SSH using Paramiko in Python - https://www.kite.com/python/answers/how-to-ssh-using-paramiko-in-python

[4] Paramiko- How to SSH and transfer files with python | by Mokgadi Rasekgala | Medium - https://medium.com/@keagileageek/paramiko-how-to-ssh-and-file-transfers-with-python-75766179de73