First, you better use port forwarding (aka SSH tunnel) to connect to a server via another server.
See Nested SSH using Python Paramiko.
Anyway to answer your literal question:
OpenSSH
sshneeds terminal when prompting for a password, so you would need to setget_ptyparameter ofSSHClient.exec_commandSSHClient.exec_command(that can get you lot of nasty side effects).Then you need to write the password to the command (
ssh) input.And then you need to write the (sub)commands to the
sshinput.
See Execute (sub)commands in secondary shell/command on SSH server in Python Paramiko.
stdin, stdout, stderr = client.exec_command(cmd, get_pty=True) stdin.write('password\n') stdin.flush() stdin.write('subcommand\n') stdin.flush() But this approach is error prone in general.