我正在编写一个jython代码,它使用Jsch在远程机器上运行一些shell指令。在继续在本地计算机上执行jython代码之前,我需要等待特定的shell指令完成(=需要在远程计算机上同步shell代码)。在
为此,当特定的shell指令完成后,我在远程机器中创建一个文本文件。然后在我的jython代码中,我打开一个sftp通道并定期尝试获取该文件(使用sftp.异常以及“while true”循环)。代码等待文件下载。在
我的问题是,即使文件被创建,jsch函数“get”也无法下载文件并继续发送sftp.异常:没有这样的文件或目录“。这使我被锁在一个无限循环中。在
这是我的代码:#We generate the info files for each class of the classlist
for classname in classlist:
print ("Generating information file (format : .out) for the class %s " % (classname))
#We open a shell channel and run the commands "windchill shell" and then "InfoReport.sh classname".
channel_generate = gl_session.openChannel('shell')
ops = channel_generate.getOutputStream()
ps = PrintStream(ops, True)
channel_generate.connect()
ps.println("windchill shell")
ps.println('rm ' + self.wt_home + '/tmp/' + classname + '.txt')
#The following instruction is the one i need to synchronize
ps.println(self.wt_home + '/bin/' + self.command + " " + classname)
#I create a file to detect the end of the previous instruction
ps.println('echo ok > ' + self.wt_home + '/tmp/' + classname)
ps.println('exit')
ps.close()
#Here we wait until the synchro file is created.
#We connect an sftp server and test every 4 seconds if we can retrieve the file.
channel_synchro = gl_session.openChannel('ftp')
channel_synchro.connect()
#This loop is broken when the get is successful
while True:
try:
channel_synchro.get(self.wt_home + '/tmp/' + classname,'/home/dev/temp')
break
except jsch.SftpException:
time.sleep(4)
#Disconnect and close de sftp channel
channel_synchro.disconnect()
channel_synchro.close()
#Disconnect and close the shell channel
channel_generate.disconnect()
channel_generate.close()
我尝试过不同的解决方案:其他类型的循环,在“while true”循环的每次迭代中创建一个新通道,我考虑过用户权限问题,我检查了路径是否准确,是否正确,我检查了我的机器的sftp功能是否正常工作。
什么都不管用,我还是解决不了这个问题