python sshclient 挂起 linux 命令

python sshclient 挂起 linux 命令

执行第二个命令时挂起,它适用于简单的 Linux 命令,如“pwd”、“ls”等,以及对这个问题的任何想法,

from paramiko import SSHClient
import paramiko, time, psutil

ssh_client = SSHClient()
ssh_client.load_system_host_keys()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hsp_host_ip, port=22, username='xx-xxx',   password='xxxxxxxxxxxx')

stdin, stdout, stderr = ssh_client.exec_command('pwd')
print(stdout.read())
print(stderr.read())

#execution hanging here and no response

stdin, stdout, stderr = ssh_client.exec_command("sudo /usr/share/source/pyenv36/lib/python3.6/site-packages/Gatewaydist/common_utils/btcli_bin/btcli /dev/ttyUSB0 h4 115200 -q")
print(stderr.read())
print(stdout.read())
print(stdin.read())

答案1

我发现问题在于执行挂在 stderr.read()/read() 处,其中通道尚未关闭,类似于此:https://stackoverflow.com/questions/35266753/paramiko-python-module-hangs-at-stdout-read

相关内容