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