Python uses Telnet and FTP to access Linux Server__linux

Source: Internet
Author: User

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! ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.