【发布时间】:2018-06-27 01:35:29
【问题描述】:
进程完成后,Plink.exe 不会关闭。代码在尝试通过检查 if output == ' ': 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
【问题讨论】:
标签: python python-2.7 ssh popen plink