add by zhj:其實兩種方案區別很小,就是在stdout讀取時有細微的區別
原文:https://www.cnblogs.com/t-road/p/13963759.html
作者:道霖
方案一:
ssh_client = paramiko.SSHClient()ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname=ip, port=port, username='root', password=destpass) stdin, stdout, stderr = ssh_client.exec_command("sh a.sh", get_pty=True) while not stdout.channel.exit_status_ready(): result = stdout.readline()
print result
# 由於在退出時,stdout還是會有一次輸出,因此需要單獨處理,處理完之後,就可以跳出了 if stdout.channel.exit_status_ready(): a = stdout.readlines() print a
break ssh_client.close()
方案二: