当stdout没有输出时,如何杀死plink.exe?

银河力量

该过程完成后,Plink.exe不会关闭。当尝试通过检查输出=='':break来中断while循环时,代码卡住了,但还是没有运气。我能够打印实时输出,但是在完成时无法中断循环。

#Establish ssh connection to a server
def _establish_connection(self):
    connection='plink -ssh {}@{} -pw {}'.format(self.user,self.server,self.pw)
    self.sp = Popen(connection,stdin=PIPE,stdout=PIPE) 



#Trying to read live output from stdout and write to file
def _test_create_log(self,_system,_logdir):
     self._establish_connection()
     self.sp.stdin.write(_command)
     timestr = time.strftime("%Y%m%d-%H%M%S")
     dir_to_log = os.path.join(_logdir,_system + '_' + timestr + '.txt')
     with open(dir_to_log,'w+') as myLog:
         while True:
             output = self.sp.stdout.readline()
             output.decode('ASCII')
             if output != '': 
                 myLog.write(output)
                 print(output.strip())
             else: #Code does not reach here, plink not killed.
                 self.sp.kill()
                 break
马丁·普里克里(Martin Prikryl)

Shell是带有输入和输出的黑匣子。无法检测到某个特定命令的输出已结束。您所能做的就是在输出的末尾添加一些魔术字符串,然后进行查找。无论如何,这不是正确的解决方案。


如果希望Plink通过该命令关闭,请在Plink命令行上提供该命令,例如:

connection='plink -ssh {}@{} -pw {} {}'.format(self.user,self.server,self.pw,_command)

尽管更好的方法是在Python中使用Paramiko等本地SSH库,而不是驱动外部控制台应用程序。

参见python paramiko run命令

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章