SoFunction
Updated on 2024-11-12

python program to download log files from a remote server

##################################################################
# sign in the ftp server and download the log file.
# Login to production server to download logs
#################################################################
def getServerLog(dir,fileName,host,userName,password):
 if (fileName):
 print '****the file '+ fileName +' has already exist! The file will be over writed'
 #connect
 try:
 f=(host)
 except (,),e:
 print '----ERROR:cannot reach '+host
 print e
 return False
 #login
 try:
 (user=userName,passwd=password)
 except ftplib.error_perm ,e:
 print '----ERROR:cannot login to server '+host
 print e
 ()
 return False
 print '****Logged in as ' + userName + ' to server ' +host
 #change folder
 try:
 (dir)
 except ftplib.error_perm,e:
 print '----ERROR:cannot CD to %s on %s' % (dir,host)
 print e
 ()
 return False
 print '**** changed to %s folder on %s' % (dir,host)
 #get file
 try:
 ('RETR %s' % fileName,open(fileName,'wb').write)
 except ftplib.error_perm,e:
 print '----ERROR:cannot read file %s on %s' % (fileName,host)
 print e
 (fileName)
 return False
 else:
 print '****Downloaded '+ fileName +' from '+ host +' to '+()
 ()
 return True

if __name__ == "__main__":
 getServerLog("/userhome/root/other/temp","","10.10.10.10","root","password")
 print '****done'