Python: Download a log program from a remote server

Source: Internet
Author: User

This article provides a method that can capture the server's log files to the local machine and automatically analyze the log files according to the program. It is often used for data mining and production maintenance.

Import osimport sysimport ftplibimport socket ################################### ############################### sign in the FTP server and download log File. # log onto the production server to download the log ################################## ############################## def getserverlog (Dir, filename, host, username, password): If OS. path. exists (filename): Print **** the file '+ filename + 'has already exist! The file will be over writed '# connect try: F = ftplib. FTP (host) socket T (socket. error, socket. gaierror), E: print '---- error: cannot reach' + host print e return false # login try: f. login (user = username, passwd = PASSWORD) Login T ftplib. error_perm, E: print '---- error: cannot login to Server' + host print e f. quit () return false print ***** logged in as '+ username + 'to Server' + host # change folder try: f. CWD (DIR) cipher t ftplib. error_perm, E: print '---- error: cannot CD to % s on % s' % (Dir, host) print e f. quit () return false print '***** changed to % s folder on % s' % (Dir, host) # Get File try: f. retrbinary ('retr % s' % filename, open (filename, 'wb '). write) Before t ftplib. error_perm, E: print '---- error: cannot read file % s on % s' % (filename, host) print e OS. unlink (filename) return false else: Print ***** downloaded '+ filename + 'from' + host + 'to' + OS. getcwd () F. quit () return true if _ name _ = "_ main _": getserverlog ("/userhome/root/other/Temp", ". out "," 10.10.10.10 "," root "," password ") print '***** done'

 

Run: Python getserverlog. py

 

Related Article

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.