Upload the Telnet and FTP downloads as functions:
#encoding =utf-8
def do_telnet (Host, username, password, finish, commands):
Import Telnetlib
Telnet remote login: Windows Client connection Linux server '
# Connect Telnet Server
TN = Telnetlib. Telnet (Host, port=23, timeout=10)
Tn.set_debuglevel (1)
# Enter login user name
Tn.read_until (' Login: ')
Tn.write (username + ' \ n ')
# Enter Login password
Tn.read_until (' Assword: ')
Tn.write (password + ' \ n ')
# Execute command after login completed
Tn.read_until (Finish)
For command in commands:
Tn.write ('%s\n '% command)
#执行完毕后, terminate Telnet connection (or enter exit exit)
Tn.read_until (Finish)
Tn.close () # tn.write (' exit\n ')
def do_ftp_download (host,username,password,localfile,remotefile):
Import Ftplib
"Connect the remote Linux server"
Ftp=ftplib. FTP ()
Ftp.connect (host,port=21)
#ftp. Set_debuglevel (2)
"' Longin '"
Ftp.login (Username,password)
FTP.SET_PASV (0)
f = open (LocalFile, ' WB ')
Ftp.retrbinary (' RETR ' + remotefile, f.write, 1024)
F.close ()
Ftp.close ()
def do_ftp_upload (host,username,password,localfile,remotefile):
Import Ftplib
Ftp=ftplib. FTP (host)
Ftp.login (Username,password)
#ftp. Set_debuglevel (2)
f = open (LocalFile, ' RB ')
Ftp.storbinary (' STOR ' + remotefile,f)
Ftp.close ()
F.close ()
If __name__== ' __main__ ':
# Configuration options
#import SYS
#params =sys.argv[0]
Import time
Params=raw_input ("Plsease input your Analyis file:\n")
Host = ' 135.252.181.62 ' #Telnet服务器IP
Username = ' Liutaili ' #登录用户名
Password = ' Liutaili ' #登录密码
finish = ' liutaili@bl8062 ' #命令提示符
commands = [' CD Parser;dos2unix%s;. /smartparser.sh%s '% (params,params)]
Begin_time=int (Time.time ())
Timearray=time.localtime (Begin_time)
Begin_otherstyletime=time.strftime ("%y-%m-%d%h:%m:%s", Timearray)
Do_ftp_upload (Host,username,password,params, '/home/liutaili/parser/%s '% params)
Print ("Upload file OK")
Do_telnet (Host, username, password, finish, commands)
Print ("Analysis file Done")
Do_ftp_download (Host,username,password, "%s.status.txt"% params, "/home/liutaili/parser/%s.status.txt"% params)
Print ("Do analysi Done")
End_time=int (Time.time ())
Timearray=time.localtime (End_time)
End_otherstyletime=time.strftime ("%y-%m-%d%h:%m:%s", Timearray)
Print "--------------------------------------------"
Print "---Begin time:%s"% Begin_otherstyletime
Print "---End time:%s"% End_otherstyletime
Print "--------------------------------------------"
Print "--------------------------------------------"
Print ("The result file is:%s.status.txt"% params)
Print "--------------------------------------------"
Raw_input (' Press any key to exit! ')