What is paramiko, and how do you run a command over SSH with it?
BeginnerConceptKubernetes, Docker & SSH clients
Answer
paramiko is a pure-Python SSHv2 implementation.
import paramiko
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.RejectPolicy())
ssh.connect("10.0.1.15", username="ec2-user", key_filename="~/.ssh/ops.pem", timeout=10)
stdin, stdout, stderr = ssh.exec_command("uptime")
rc = stdout.channel.recv_exit_status()
print(rc, stdout.read().decode())
ssh.close()
AutoAddPolicy is convenient but trusts any host key on first connect (MITM risk); in production pre-populate known_hosts and reject unknown keys.
More on Kubernetes, Docker & SSH clients
How does the official Kubernetes Python client authenticate?BeginnerHow do you list pods with high restart counts using the Python client?BeginnerHow do you patch or scale a Deployment with the Python client?IntermediateHow does `watch.Watch()` work, and what is a `410 Gone`?AdvancedHow do you apply a YAML manifest from Python?AdvancedHow do you use the Docker SDK for Python?Beginner