ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(server_IP,22,username, password)
stdin, stdout, stderr = ssh.exec_command('/Users/lteue/Downloads/uecontrol-CXC_173_6456-R32A01/uecontrol.sh -host localhost ')
alldata = ""
while not stdout.channel.exit_status_ready():
solo_line = ""
# Print stdout data when available
if stdout.channel.recv_ready():
# Retrieve the first 1024 bytes
solo_line = stdout.channel.recv(1024)
alldata += solo_line
if(cmp(solo_line,'uec> ') ==0 ): #Change Conditionals to your code here
if num_of_input == 0 :
data_buffer = ""
for cmd in commandList :
#print cmd
stdin.channel.send(cmd) # send input commmand 1
num_of_input += 1
if num_of_input == 1 :
stdin.channel.send('q \n') # send input commmand 2 , in my code is exit the interactive session, the connect will close.
num_of_input += 1
print alldata
ssh.close()
如果直接使用而不检查 stdout.channel.recv_ready(): 为什么 stdout.read() 会挂起,而 stdout.channel.exit_status_ready():
就我而言,在远程服务器上运行命令后,会话正在等待用户输入,输入“q”后,它将关闭连接。但在输入 'q' 之前,stdout.read() 将等待 EOF,如果缓冲区较大,似乎此方法不起作用。
- 我在 while 中尝试了 stdout.read(1) ,它有效
我在 while 中尝试了 stdout.readline() ,它也有效。
stdin, stdout, stderr = ssh.exec_command('/Users/lteue/Downloads/uecontrol')
stdout.read() 将挂起