379 words
2 minutes
Python paramiko Module - Complete Tutorial

Python paramiko Module - Complete Tutorial#

Table of Contents#

  1. What Is paramiko
  2. Installation
  3. SSH Basics
  4. Basic SSH Connection
  5. Run Commands Remotely
  6. Authentication Options
  7. SFTP File Transfer
  8. Advanced Features
  9. Error Handling
  10. SSH Security
  11. Cybersecurity Use Cases
  12. Quick Reference

What Is paramiko#

paramiko is a Python implementation of SSHv2. It lets you connect to remote servers, run commands, and transfer files over SFTP.

Common uses:

  • Remote administration
  • Deployment automation
  • Secure file transfers
  • SSH based tooling

Installation#

Terminal window
pip install paramiko

Optional pin in requirements.txt:

paramiko==3.3.1

SSH Basics#

Key concepts:

  • Host: remote server address
  • Port: SSH default is 22
  • Username and password or SSH key
  • Host key: verifies server identity

Basic SSH Connection#

Password authentication#

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(
hostname="192.168.1.100",
port=22,
username="admin",
password="secret123",
timeout=10
)
print("Connected")
finally:
ssh.close()

Run Commands Remotely#

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.1.100", username="admin", password="secret123")
stdin, stdout, stderr = ssh.exec_command("whoami")
output = stdout.read().decode()
error = stderr.read().decode()
exit_code = stdout.channel.recv_exit_status()
print(output)
print(error)
print(exit_code)
ssh.close()

Authentication Options#

SSH key authentication#

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
key = paramiko.RSAKey.from_private_key_file("/home/user/.ssh/id_rsa")
ssh.connect("192.168.1.100", username="admin", pkey=key)
ssh.close()

SSH key with passphrase#

key = paramiko.RSAKey.from_private_key_file(
"/home/user/.ssh/id_rsa",
password="key-passphrase"
)

Host key verification#

import paramiko
ssh = paramiko.SSHClient()
ssh.load_host_keys("/home/user/.ssh/known_hosts")
ssh.set_missing_host_key_policy(paramiko.RejectPolicy())
ssh.connect("192.168.1.100", username="admin", password="secret123")
ssh.close()

SFTP File Transfer#

Upload#

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.1.100", username="admin", password="secret123")
sftp = ssh.open_sftp()
sftp.put("/home/user/data.txt", "/tmp/data.txt")
sftp.close()
ssh.close()

Download#

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.1.100", username="admin", password="secret123")
sftp = ssh.open_sftp()
sftp.get("/var/log/syslog", "/home/user/syslog.txt")
sftp.close()
ssh.close()

Advanced Features#

Interactive shell#

import paramiko
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.1.100", username="admin", password="secret123")
channel = ssh.invoke_shell()
channel.send("uname -a\n")
while not channel.recv_ready():
time.sleep(0.1)
print(channel.recv(1024).decode())
channel.close()
ssh.close()

SSH tunnel (local port forward)#

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.1.100", username="admin", password="secret123")
transport = ssh.get_transport()
transport.request_port_forward("127.0.0.1", 3307)
print("Tunnel open on 127.0.0.1:3307")

Error Handling#

import paramiko
import socket
from paramiko import AuthenticationException
from paramiko.ssh_exception import SSHException
def safe_connect(host, user, password):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname=host, username=user, password=password, timeout=10)
return client
except AuthenticationException:
print("Auth failed")
except SSHException as e:
print(f"SSH error: {e}")
except socket.timeout:
print("Timeout")
except Exception as e:
print(f"Unexpected error: {e}")
return None

SSH Security#

Best practices:

  • Use SSH keys, not passwords
  • Verify host keys
  • Disable auto-accept in production
  • Rotate credentials regularly

Cybersecurity Use Cases#

import paramiko
def get_banner(host, port=22):
transport = paramiko.Transport((host, port))
transport.start_client()
banner = transport.remote_version
transport.close()
return banner
print(get_banner("192.168.1.100"))

Brute force demo (authorized testing only)#

import paramiko
import time
def ssh_bruteforce(host, user, passwords):
for pwd in passwords:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname=host, username=user, password=pwd, timeout=3)
return pwd
except paramiko.AuthenticationException:
pass
finally:
client.close()
time.sleep(0.5)
return None

Quick Reference#

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("host", username="user", password="pass")
stdin, stdout, stderr = ssh.exec_command("ls -la")
output = stdout.read().decode()
sftp = ssh.open_sftp()
sftp.put("local.txt", "/tmp/remote.txt")
sftp.get("/tmp/remote.txt", "downloaded.txt")
sftp.close()
ssh.close()