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.read()将挂起,如果使用dierectly而不检查标准输出。 channel.recv_ready():在while stdout.channel.exit_status_ready():
对我来说,在远程服务器上运行命令后,会话正在等待用户输入,之后在把'Q',它会关闭连接。 但在输入'q'之前,stdout。read()将等待EOF,看起来这个方法在缓冲区较大时不起作用。
- 我在试图同时stdout.read(1),它的工作原理
我试图同时stdout.readline(),它的工作原理也。
标准输入,标准输出,标准错误= ssh.exec_command( '/用户/ lteue /下载/ uecontrol')
stdout.read()将挂起
的问题是旧的,但为人民谁仍然来到这里通过谷歌搜索我想给他们this.The关键是要得到自己的**频道** [通过Paramiko第1部分在Python中执行交互命令](https://www.youtube.com/watch?v=Jc2l- n_GYPI)[通过Paramiko第2部分在Python中执行交互命令](https://www.youtube.com/watch?v=lLKdxIu3-A4)在这两个视频中,解释了如何通过paramiko运行交互命令,特别是第二个视频是很好,可能你需要什么。 – 2015-01-26 16:50:10