Python windows计算机上的paramiko ssh挂起在packet.py中
我在Linux和Windows上工作。 正如这里在许多地方所写的,我使用stdout.channel.eofèu接收的解决方法来避免parmaiko挂在packet.py上。 我的Linux SSH代码:Python windows计算机上的paramiko ssh挂起在packet.py中,python,windows,ssh,paramiko,Python,Windows,Ssh,Paramiko,我在Linux和Windows上工作。 正如这里在许多地方所写的,我使用stdout.channel.eofèu接收的解决方法来避免parmaiko挂在packet.py上。 我的Linux SSH代码: def SendCommand(self, Command, GetOutput=False,timeout=20): log.info('SSH : ' + inspect.currentframe().f_code.co_name + '\nWith: ' + str(locals
def SendCommand(self, Command, GetOutput=False,timeout=20):
log.info('SSH : ' + inspect.currentframe().f_code.co_name + '\nWith: ' + str(locals().items()))
stdin, stdout, stderr = self.ssh.exec_command("echo '{}' | sudo -S {}".format(self.password, Command))
resp = ''
resperr = ''
if GetOutput:
endtime = time.time() + timeout
while not stdout.channel.eof_received:
time.sleep(0.5)
if time.time() > endtime:
stdout.channel.close()
break
outlines = stdout.readlines()
outlinesstderr = stderr.readlines()
resp = ''.join(outlines)
resperr = ''.join(outlinesstderr)
stdinstat, stdoutstat, stderrstat = self.ssh.exec_command('if [ $? -eq 0 ]; then echo OK; else echo FAIL; fi')
outlines = stdoutstat.readlines()
respstat = ''.join(outlines)
log.info(f'SSHlog {Command} status:{respstat}')
return [resp, resperr, respstat]
它工作得很好
但是。。。当我打开SSH到Windows时,此解决方案不起作用。
我从来没有到达EOF扩展,我总是走出循环超时。。。
有人有其他解决办法的想法吗?我目前正在运行一个python over ssh脚本,当我尝试使用readline()函数读取时,它有时会挂起
我的windows SSH代码:
def send_windows_command_getResp(self, command):
timeout = 1
ssh_stdin, ssh_stdout, ssh_stderr = self.ssh.exec_command(command)
endtime = time.time() + timeout
while not ssh_stdout.channel.eof_received:
time.sleep(0.5)
if time.time() > endtime:
ssh_stdout.channel.close()
break
response = ssh_stdout.readline().rstrip()
return response
谢谢