Advertisement
Guest User

Untitled

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