Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
4 of 7
edited body
Martin Prikryl
  • 205k
  • 64
  • 560
  • 1.1k

First, you better use port forwarding (aka SSH tunnel) to connect to a server via another server.

There's a ready-made forward_tunnel function in Paramiko forward.py demo exactly for this purpose.

See also Port forwarding with Paramiko.


Anyway to answer your literal question:

  1. OpenSSH ssh needs terminal when prompting for a password, so you would need to set get_pty parameter of SSHClient.exec_command (that can get you lot of nasty side effects).

  2. Then you need to write the password to the command (ssh) input.

  3. And then you need to write the (sub)commands to the ssh input.
    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 is approach is error prone in general.

Martin Prikryl
  • 205k
  • 64
  • 560
  • 1.1k