Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Paramiko通过另一台服务器连接到服务器_Python_Ssh_Paramiko - Fatal编程技术网

Python 使用Paramiko通过另一台服务器连接到服务器

Python 使用Paramiko通过另一台服务器连接到服务器,python,ssh,paramiko,Python,Ssh,Paramiko,我试图使用Paramiko进入服务器,然后进入服务器中的路由器,然后运行命令 但是,我没有得到路由器的密码输入,然后它只是关闭连接 username, password, port = ... router = ... hostname = ... client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.WarningPolicy) clien

我试图使用Paramiko进入服务器,然后进入服务器中的路由器,然后运行命令

但是,我没有得到路由器的密码输入,然后它只是关闭连接

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()

有什么帮助吗?

首先,您最好使用端口转发(又称SSH隧道)通过另一台服务器连接到服务器


无论如何,要回答您的字面问题:

  • OpenSSH
    ssh
    在提示输入密码时需要终端,因此您需要设置的
    get_pty
    参数(这可能会给您带来很多讨厌的副作用)

  • 然后需要将密码写入命令(
    ssh
    )输入

  • 然后需要将(sub)命令写入
    ssh
    输入。

  • 但这种方法通常容易出错

    stdin, stdout, stderr = client.exec_command(cmd, get_pty=True) 
    stdin.write('password\n')
    stdin.flush()
    stdin.write('subcommand\n')
    stdin.flush()