37,741
社区成员
发帖
与我相关
我的任务
分享
import paramiko
import time
import re
sshClient=paramiko.SSHClient()
sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())#跳过远程连接中选择“是”的环节
cmd=r'pwd'
enterStr="\n"
try:
sshClient.connect('135.***.***.***',22,'MyUserName','MyPassword')
pass
except BaseException as ex:
print(str(ex))
print("Pres any key to exit...")
input()
exit()
pass
myChannel=sshClient.invoke_shell()
while True:
myChannel.send(cmd+enterStr)
time.sleep(0.5)
result=myChannel.recv(4096)
stdin, stdout, stderr = sshClient.exec_command(command)
recv_ready()
Returns true if data is buffered and ready to be read from this channel. A False result does not mean that the channel has closed; it means you may need to wait before more data arrives.
Returns: True if a recv call on this channel would immediately return at least one byte; False otherwise.
那么你就可以这样, 直接看信道是否 recv_ready(), 返回True 就recv(), 如果False 就等待个0.2秒再判断
但是这个recv_ready()并不能帮助我们判断接收是否结束了
至于判断是否接收完毕,我也没啥好想法, 我现在的笨办法就是看recv() 到的信息内是否出现路由器交换机的主机名(比如思科就是xxxx#, 华为华三就是 <xxxxx>), 如果出现了,说明设备回显结束了,可以break这个接收循环.
设备主机名可以用先recv()下,保证没有多余字符后,发送回车,再次recv()得到.
确实比较蠢, 如果有更合适的办法烦请告诉我,谢谢!