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.
edited body
Source Link
Martin Prikryl
  • 205k
  • 64
  • 560
  • 1.1k

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:

  1. OpenSSH ssh needs terminal when prompting for a password, so you would need to set get_pty parameter of SSHClient.exec_commandSSHClient.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 this approach is error prone in general.

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:

  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 this approach is error prone in general.

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:

  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 this approach is error prone in general.

deleted 385 characters in body
Source Link
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.

Once you forward the port, you connect another Paramiko SSHClient instance to the forwarded port – local_port in the @Y answer to the above question (and localhost or 127.0.0.1 as host)Nested SSH using Python 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 isthis approach is error prone in general.

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.

Once you forward the port, you connect another Paramiko SSHClient instance to the forwarded port – local_port in the @Y answer to the above question (and localhost or 127.0.0.1 as host).


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.

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:

  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 this approach is error prone in general.

added 198 characters in body
Source Link
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.

Once you forward the port, you connect another Paramiko SSHClient instance to the forwarded port – local_port in the @Y answer to the above question (and localhost or 127.0.0.1 as host).


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.

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.

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.

Once you forward the port, you connect another Paramiko SSHClient instance to the forwarded port – local_port in the @Y answer to the above question (and localhost or 127.0.0.1 as host).


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.

edited body
Source Link
Martin Prikryl
  • 205k
  • 64
  • 560
  • 1.1k
Loading
edited body
Source Link
Martin Prikryl
  • 205k
  • 64
  • 560
  • 1.1k
Loading
added 46 characters in body
Source Link
Martin Prikryl
  • 205k
  • 64
  • 560
  • 1.1k
Loading
Source Link
Martin Prikryl
  • 205k
  • 64
  • 560
  • 1.1k
Loading