Interview Prep/Python for DevOps/Kubernetes, Docker & SSH clients

How do you SSH through a bastion host from Python?

AdvancedConceptKubernetes, Docker & SSH clients
Answer

With Fabric, pass a gateway Connection:

bastion = Connection("bastion.acme.com", user="ops")
c = Connection("10.0.2.20", user="ec2-user", gateway=bastion)
c.run("hostname")

With paramiko, open a direct-tcpip channel on the bastion's transport and pass it as sock= when connecting to the target. On AWS, many teams avoid bastions entirely with SSM Session Manager (aws ssm start-session, or ssm.send_command from boto3 for remote command execution without any inbound SSH).

All Python for DevOps questions Roadmap: Python for automation

More on Kubernetes, Docker & SSH clients