#!/usr/bin/python
import smtplib
from email.mime.text import MIMEText
# This is the send mail function
# it takes 3 strings: destination address, subject, and text as input
# Fill here your email account details i use the same server.
def send_mail(to_address, subject, text):
from_address = '[email protected]'
msg = MIMEText(text)
msg['subject'] = subject
msg['From'] = from_address
msg['To'] = to_address
s = smtplib.SMTP('localhost:587')
s.ehlo()
s.starttls()
s.login('[email protected]', 'your_email_password')
s.sendmail(from_address, to_address, msg.as_string())
s.quit
import os
import subprocess
import re
import string
# this list used to exclude files that starts with this string
# keep in mind that you place here the path after /home/user/public_html
# for example if you want to exclude /home/user/public_html/wp-content/cache you just write '/wp-content/cache'
excluded_paths = ['/wp-content/cache',
'/wp-content/plugins/si-contact-form/captcha/cache',
'/magento/var/cache',
'/assets/cache',
]
# It takes the output of find command in one string,
def check_lines(output, userpath):
new_output = ""
# split the output string to check line by line
lines = output.split('\n')
for line in lines:
exclude_it = False
# here is where the exclusion list filter works
#
for ex_path in excluded_paths:
if (re.match(userpath + ex_path , line) != None):
exclude_it = True
break
# filter out files ends with error_log
if (re.match(r'(.*error_log$)', line) != None):
exclude_it = True
if (exclude_it == False) and (line !=''):
new_output += line + '\n'
return new_output
user_list = os.listdir("/var/cpanel/users/")
message = ""
for user in user_list:
userpath = "/home/" + user + "/public_html"
# here is the find command, -182 is how many minutes back
# i choose to see changes every 3 hours, i run this script every 3 hours on my server thats why i have 182
p = subprocess.Popen(["find", userpath, "-name", "*", "-mmin", "-182", "-print"], stdout = subprocess.PIPE)
output, err = p.communicate()
if (output != ""):
processed_output = check_lines(output, userpath)
if (processed_output != ''):
message += "username: " + user + '\n'
message += processed_output + '\n'
# where to send the email and what subject
send_mail('[email protected]', 'File changes', message)