paramiko模块,链接服务器,启动进程,打印输出结果,卡住了

# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname=hostname,port=22, username=username, password=password)

# 执行命令
stdin, stdout, stderr = ssh.exec_command('sh run.sh')

# 获取命令结果
result = stdout.read()
ssh.close()
print(result)

 

执行的命令是  启动脚本,没有任何的输出,在运行的时候,卡在了   result = stdout.read(),一直没有执行下一步

当stdout中没有数据或有一行没有输出l时(即在sh脚本中的read语句中),就会发生这种情况。尝试设置“get_pty=True”,然后只读取stdout中的字节

stdin, stdout, stderr = ssh.exec_command('sh run.sh',get_pty=True)
原文地址:https://www.cnblogs.com/xiaofeng91/p/15070097.html