使用telnet在QNX提示符上执行二进制文件时确定等待时间

时间:2014-10-09 21:29:57

标签: python-2.7 sleep telnet qnx

我正在使用QNX上的sloginfo处理某些输出二进制文件,我使用ftp / telnet / vmware来到我从机器上传二进制文件到vmware实例然后运行sloginfo命令。 问题是需要处理的二进制文件大小不一致(范围从50mb到200mb),处理这些文件所需的时间不同,因此无法确定所需的等待/休眠时间。

我需要知道sloginfo是否返回一个可以用作标志的值。我尝试使用tn.read_until(),但没有获得所需的结果。

import os, sys, telnetlib, time
from ftplib import FTP

def upload(ftp, filed):
      ext = os.path.splitext(filed)[1]
      if ext in (".txt", ".htm", ".html"):
               ftp.storlines("STOR " + filed, open(filed))
      else:
               ftp.storbinary("STOR " + filed, open(filed, "rb"), 1024)

def gettext(ftp, filename, outfile=None):
  # fetch a text file
  if outfile is None:
      outfile = sys.stdout
  # use a lambda to add newlines to the lines read from the server
  ftp.retrlines("RETR " + filename, lambda s, w=outfile.write: w(s+"\n"))

if __name__ == '__main__':
dbfile = "LOG1"

nonpassive = False
remotesite = '192.168.0.128'
ftp_port = '21'
tel_port = '23'
password = 'root'

ftp = FTP()
ftp.connect(remotesite, ftp_port)

ftp.login('root','root')

print 'Uploading the Log file... Please wait...'
upload (ftp, dbfile)
print 'File Uploaded Successfully...'

tn = telnetlib.Telnet(remotesite, tel_port)

tn.read_until("login: ")

tn.write('root' + "\n")
if password:
    tn.write(password + "\n")

tn.write("sloginfo LOG1 >> LOG1.txt\n")

**#need to get more control on this sleep time**
time.sleep(300)
print 'Downloading text file...'
gettext(ftp, "LOG1.txt", open(r'LOG1.txt','wb'))


ftp.close()
tn.close()

1 个答案:

答案 0 :(得分:0)

tn.write(“sloginfo LOG1>> LOG1.txt \ n”)用tn.write修改了上述注释('sloginfo'+ strdbfile +'>>'+ strdbfiletxt +'; echo Done!\ n')这解决了问题