python 使用telnet和ftp訪問linux server__linux

來源:互聯網
上載者:User

將telnet和ftp的下載上傳封裝成函數:


#encoding=utf-8

def do_telnet(Host, username, password, finish, commands):
    import telnetlib
    '''Telnet遠程登入:Windows用戶端串連Linux伺服器'''
 
    # 串連Telnet伺服器
    tn = telnetlib.Telnet(Host, port=23, timeout=10)
    tn.set_debuglevel(1)
     
    # 輸入登入使用者名稱
    tn.read_until('login: ')
    tn.write(username + '\n')
    
    # 輸入登入密碼
    tn.read_until('assword: ')
    tn.write(password + '\n')
      
    # 登入完畢後執行命令
    tn.read_until(finish)
    for command in commands:
        tn.write('%s\n' % command)
    
    #執行完畢後,終止Telnet串連(或輸入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__':
# 配置選項
#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!')

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.