I am trying to get into a server using Paramiko and then get into a router that's in the server and then run a command.
However, I am not getting a password input for the router and then it just closes the connection.
username, password, port = ...
router = ...
hostname = ...
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect(hostname, port = port, username = username, password = password)
cmd = # ssh hostname@router
# password input comes out here but gets disconnected
stdin, stdout, stderr = client.exec_command(cmd)
HERE # command to run in the router
stdout.read()
client.close()
Any help?
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 Paramikoforward.py
demo exactly for this purpose.See also Port forwarding with Paramiko.
Anyway to answer your literal question:
OpenSSH
ssh
needs terminal when prompting for a password, so you would need to setget_pty
parameter ofSSHClient.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
ssh
input.See Execute (sub)commands in secondary shell/command on SSH server in Python Paramiko.
But is approach is error prone in general.