Advertisement
Guest User

Untitled

a guest
Jun 15th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.00 KB | None | 0 0
  1. __author__="Diego Rodriguez"
  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 server
  17.   self.ftp_user=""#FTP user
  18.   self.ftp_pass=""#FTP pass
  19.   self.smtp_server="smtp.gmail.com"#SMTP server (irecomend gmail ;))
  20.   self.smtp_user=""#SMTP user
  21.   self.smtp_pass=""# SMTP password
  22.   self.directorio2move="/Mysecretlogfolder"# directory where logs files will be moved
  23.   self.TUEMAIL = ""# your email
  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: LeXeL : lexelEZ[at]gmail[dot]com\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.     def mover(self):
  64.   if "Mysecretlogfolder" in os.listdir("/"):
  65.     for dirc in self.archivosLogs:
  66.     try:
  67.     shutil.move(dirc,self.directorio2move)
  68.  
  69.     except:
  70.     print "There was an Error\nPlease check this:"
  71.     print "\t[-]The directory %s" %(self.directorio2move)
  72.     print "\t[-]check if the file already exist's"
  73.     sys.exit(1)
  74.   elif "Mysecretlogfolder" not in os.listdir("/"):
  75.     os.system("mkdir %s" %(self.directorio2move))
  76.     for dirc in self.archivosLogs:
  77.     try:
  78.     shutil.move(dirc,self.directorio2move)
  79.     except:
  80.     print "There was an Error\nPlease check this:"
  81.     print "\t[-]The directory %s" %(self.directorio2move)
  82.     print "\t[-]check if the file already exist's"
  83.     sys.exit(1)
  84.   else:
  85.     print "[-] Could't create the directory!"
  86.  
  87.     def info(self):
  88.   now = datetime.datetime.now()
  89.   now =str(now)
  90.   d = ""
  91.   for dic in self.archivosLogs:
  92.     d += "We have found a Log file in: %s\n" %(dic)
  93.     a =  "The log file have been moved to %s" %(self.directorio2move)
  94.   return """Your local Time is :%s\n %s \n %s  """  %(now,d,a)
  95.  
  96.     def correo(self):
  97.   if self.SMTPTF == True:
  98.     smtp = smtplib.SMTP('smtp.gmail.com',25)
  99.     smtp.ehlo()
  100.     smtp.starttls()
  101.     smtp.ehlo()
  102.     smtp.login(self.smtp_user,self.smtp_pass)
  103.     mensaje = MIMEText(self.info())
  104.     mensaje['From']=self.smtp_user
  105.     mensaje['To']=self.TUEMAIL
  106.     mensaje['Subject']="Se ha Encontrado un Log File en tu systema"
  107.     smtp.sendmail(self.smtp_user,self.TUEMAIL,mensaje.as_string())
  108.  
  109.     def conecter(self):
  110.   self.titulo()
  111.   fserver = self.ftp_server
  112.   fuser=self.ftp_user
  113.   fpass=self.ftp_pass
  114.   sserver=self.smtp_server
  115.   suser=self.smtp_user
  116.   spass=self.smtp_pass
  117.   try:
  118.     ftp = FTP(fserver)
  119.     ftp.login(user=fuser,passwd=fpass)
  120.   except BaseException, e:
  121.  
  122.     print '\n [-] Error: %s ' %(e)
  123.   else:
  124.     print "[*]Sucessfuly conencted to: %s" %(fserver)
  125.   if "log" in ftp.nlst():
  126.     print "[+] Found a Log file on the FTP server"
  127.   if "log" not in ftp.nlst():
  128.     print "[+] Creating a Log directory on the FTP server"
  129.     ftp.mkd("/log")
  130.  
  131.   try:
  132.     smtp = smtplib.SMTP(sserver,25)
  133.     smtp.ehlo()
  134.     smtp.starttls()
  135.     smtp.ehlo()
  136.     smtp.login(suser,spass)
  137.   except BaseException, e:
  138.     print '\n [-] Error: %s ' %(e)
  139.     self.SMTPTF = False
  140.   else:
  141.     print "[*]Sucessfuly conencted to: %s" %(sserver)
  142.     self.SMTPTF = True
  143.  
  144.     def buscador(self):
  145.  
  146.   try:
  147.     path_to_search = raw_input("Please enter the path to scan: ")
  148.     timetowait = input("Please enter the time to sleep in seconds: ")
  149.     print "The Program Just start Please wait"
  150.     print "Found:"
  151.   except KeyboardInterrupt, IOError:
  152.     print "[-] An Error occured \n Quiting!"
  153.     sys.exit(1)
  154.   while True:
  155.     now = datetime.datetime.now()
  156.     for root, dirs, files in walk(path_to_search):
  157.     for archivo in files:
  158.     (shortname, extension) = path.splitext(archivo)  
  159.  
  160.     if extension=='.log':
  161.     formato = shortname+" | "+str(now)
  162.     archivo2 = formato+extension
  163.     self.archivosLogs.append(path.join(root, archivo))
  164.     self.archivos.append(archivo2)
  165.     self.sender()
  166.     time.sleep(timetowait)
  167.  
  168. if __name__ == '__main__':
  169.     Log = Log_finder()
  170.     Log.conecter()
  171.     Log.buscador()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement