Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__="Diego & Oscar y un especial abrazo a Ariel de Argentina por la exelente ayuda en Python"
- __date__ ="$08/16/2010 09:53:59 PM$"
- from os import path, walk
- from ftplib import FTP
- import smtplib
- import mimetypes
- from email.MIMEText import MIMEText
- import socket
- import sys
- import time
- import datetime
- import shutil
- import os
- class Log_finder(object):
- def __init__(self):
- self.ftp_server=""
- self.ftp_user=""
- self.ftp_pass=""
- self.smtp_server=""
- self.smtp_user=""
- self.smtp_pass=""
- self.directorio2move="C:\Satellite"
- self.archivosLogs=[]
- self.archivos=[]
- def sender(self):
- log_ip = "log"
- ftp = FTP(self.ftp_server)
- ftp.login(user=self.ftp_user,passwd=self.ftp_pass)
- arc = str(self.archivos)
- arc = arc.replace("['", "")
- arc = arc.replace("']", "")
- for dic in self.archivosLogs:
- print ">>>>>>> %s" %(dic)
- f = open (dic ,"rb")
- ftp.cwd("/log")
- ftp.storbinary('STOR '+arc,f)
- self.mover()
- self.correo()
- self.archivosLogs.remove(dic)
- self.archivos.remove(arc)
- def mover(self):
- if "Satellite" in os.listdir("C:\\"):
- arc = str(self.archivos)
- arc = arc.replace("['", "")
- arc = arc.replace("']", "")
- dirc = self.root+arc
- shutil.move(dirc,self.directorio2move)
- elif "Satellite" not in os.listdir("C:\\"):
- arc = str(self.archivos)
- arc = arc.replace("['", "")
- arc = arc.replace("']", "")
- os.system("mkdir %s" %(self.directorio2move))
- dirc = self.root+arc
- shutil.move(i,self.directorio2move)
- def info(self):
- now = datetime.datetime.now()
- now =str(now)
- return "Your local Time is :%s " %(now)
- for dic in self.archivosLogs:
- return "We have found a Log file in: %s" %(dic)
- return "The log file have been moved to %s" %s(self.dicerctorio2move)
- def correo(self):
- if self.SMTPTF == True:
- smtp = smtplib.SMTP('smtp.gmail.com',25)
- smtp.ehlo()
- smtp.starttls()
- smtp.ehlo()
- smtp.login(self.smtp_user,self.smtp_pass)
- mensaje = MIMEText(self.info())
- mensaje['From']=self.smtp_user
- mensaje['To']=self.TUEMAIL
- mensaje['Subject']="Se ha Encontrado un Log File"
- smtp.sendmail(self.smtp_user,self.TUEMAIL,mensaje.as_string())
- def conecter(self):
- fserver = self.ftp_server
- fuser=self.ftp_user
- fpass=self.ftp_pass
- sserver=self.smtp_server
- suser=self.smtp_user
- spass=self.smtp_pass
- try:
- ftp = FTP(fserver)
- ftp.login(user=fuser,passwd=fpass)
- except BaseException, e:
- print '\n [-] Error: %s ' %(e)
- else:
- print "[*]Sucessfuly conencted to: %s" %(fserver)
- if "log" in ftp.nlst():
- print "[+] Found a Log file on the FTP server"
- if "log" not in ftp.nlst():
- print "[+] Creating a Log directory on the FTP server"
- ftp.mkd("/log")
- try:
- smtp = smtplib.SMTP(sserver,25)
- smtp.ehlo()
- smtp.starttls()
- smtp.ehlo()
- smtp.login(suser,spass)
- except BaseException, e:
- print '\n [-] Error: %s ' %(e)
- self.SMTPTF = False
- else:
- print "[*]Sucessfuly conencted to: %s" %(sserver)
- self.SMTPTF = True
- def buscador(self):
- try:
- path_to_search = raw_input("Please enter the path to scan: ")
- timetowait = input("Please enter the time to sleep in seconds: ")
- print "Found:"
- except KeyboardInterrupt, IOError:
- print "[-] An Error occured \n Quiting!"
- sys.exit(1)
- while True:
- now = datetime.datetime.now()
- for root, dirs, files in walk(path_to_search):
- for archivo in files:
- (shortname, extension) = path.splitext(archivo)
- if extension=='.txt':
- formato = shortname+" | "+str(now)
- archivo2 = formato+extension
- self.root = root
- self.archivosLogs.append(path.join(root, archivo))
- self.archivos.append(archivo2)
- self.sender()
- time.sleep(timetowait)
- else:
- time.sleep(timetowait)
- if __name__ == '__main__':
- Log = Log_finder()
- Log.conecter()
- Log.buscador()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement