Automate SSH with paramiko implementing agent forwarding. Connect to a bastion host then hop to a private instance to execute commands. Handle SSHException for unknown host keys with auto-add policy.
Assistant
Explaining SSH agent forwarding with Paramiko
Paramiko enables SSH agent forwarding by calling AgentRequestHandler on a session channel, which lets the remote server use your local SSH agent for authentication—needed when hopping through a bastion to a private host. Setting AutoAddPolicy handles unknown host keys automatically. The typical pattern is to connect to the bastion with allow_agent=True, open a session channel, attach AgentRequestHandler to enable forwarding, then execute an SSH command on the bastion that uses agent forwarding (e.g., ssh -A target).
What ssh -A does is two things:
It authenticates to the jump host with whatever keys you have loaded in your local agent.
It tells the jump host “please forward any agent requests back down this channel to my agent”.
Paramiko will do the same, but you have to turn the pieces on yourself. At a minimum you need to:
Tell the SSHClient how to deal with missing host keys, otherwise it will throw an SSHException the first time it sees an unknown host. Using AutoAddPolicy will silently accept and store the key.
Allow use of the local ssh‑agent when you connect (allow_agent=True).
Install an AgentRequestHandler on the session channel you are going to use for the hop, so that the remote ssh process sees a working SSH_AUTH_SOCK.
One way to “hop” is simply to exec the remote ssh binary on the bastion. A complete example looks like this:
python
import paramiko
from paramiko import SSHException
bastion_host = 'bastion.example.com'
bastion_user = 'ec2-user'
target_host = '10.0.1.5'
target_user = 'ubuntu'
command = 'uname -a'
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# authenticate to the bastion using your local agent/keys
client.connect(
bastion_host,
username=bastion_user,
allow_agent=True,
look_for_keys=True,
)
except SSHException as e:
print(f'Failed to connect to bastion: {e}')
raise
# open a session channel and request agent forwarding on it
chan = client.get_transport().open_session()
paramiko.agent.AgentRequestHandler(chan)
# run ssh on the bastion to reach the private host
chan.exec_command(f'ssh -A {target_user}@{target_host} "{command}"')
# read output
stdout = []
stderr = []
while True:
if chan.recv_ready():
stdout.append(chan.recv(1024).decode())
if chan.recv_stderr_ready():
stderr.append(chan.recv_stderr(1024).decode())
if chan.exit_status_ready():
break
print(''.join(stdout))
if stderr:
print('stderr:', ''.join(stderr))
chan.close()
client.close()
If you prefer to avoid invoking a second ssh binary you can also build a “direct‑tcpip” channel through the bastion and create a second SSHClient on top of it; the important parts remain the same: set a missing host key policy and either use allow_agent=True/look_for_keys=True or explicitly load a key, and wrap the channel in AgentRequestHandler when you want agent forwarding to work.
With set_missing_host_key_policy(paramiko.AutoAddPolicy()) in place Paramiko will auto‑accept unknown host keys instead of throwing an SSHException on first contact.