#!/bin/bash
#此脚本用于检测ftp中文件是否被修改,如果被修改,发送邮件告知。
#定义验证文件目录
FileDir='/var/zhou'
#获取主机名
HostName=$(hostname)
#定义邮件参数
Mail_Smtp="smtp服务器地址"
Mail_User="邮箱用户名"
Mail_Pass="邮箱密码"
Mail_From="发件人邮箱"
Mail_To="收件人邮箱"
Mail_Subject="${HostName}:The file has been changed"
Mail_Content="${HostName}:The file has been changed."
#定义需要监测的文件夹
CheckDir=(
/usr/dir1
)
#定义生成所需验证的文件样本日志的函数
function OldFile(){
for i in ${CheckDir[@]}
do
/bin/find ${i} -type f -print0 | xargs -0 md5sum >> ${FileDir}/old.log
done
}
function NewFile(){
for i in ${CheckDir[@]}
do
/bin/find ${i} -type f -print0 | xargs -0 md5sum >> ${FileDir}/new.log
done
}
#定义邮件发送函数
function SendMail(){
/usr/local/bin/sendEmail -f $Mail_From -t $Mail_To -s $Mail_Smtp -u $Mail_Subject -xu $Mail_User -xp $Mail_Pass -m "$Mail_Content"\n
}
if [ ! -d ${FileDir} ]
then
mkdir ${FileDir} -p
fi
if [ ! -e ${FileDir}/old.log ]
then
OldFile
fi
#如果没有安装sendEmail,则安装
if [ ! -f /usr/local/bin/sendEmail ]
then
cd /usr/local/src/
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
tar -xf sendEmail-v1.56.tar.gz
cd sendEmail-v1.56
cp sendEmail /usr/local/bin
chmod 0755 /usr/local/bin/sendEmail
fi
#生成新的日志文件
NewFile
#新的日志文件与旧的日志文件进行对比
/usr/bin/diff ${FileDir}/new.log ${FileDir}/old.log > ${FileDir}/diff.log
Status=$?
if [ $Status -ne 0 ]
then
Mail_Content=`cat ${FileDir}/diff.log |awk '{if($1=="<"){printf"增加\t%s\n",$3};if($1==">"){printf"删除\t%s\n",$3}}'`
SendMail
fi
#清除新旧日志文件,备份比较结果
/bin/cp -f ${FileDir}/diff.log ${FileDir}/diff$(date +%F__%T).log
cat /dev/null > ${FileDir}/old.log
cat /dev/null > ${FileDir}/new.log
#生成新的旧日志文件
OldFile
#清除30天前的日志文件
/bin/find ${FileDir} -type f -mtime +30 |xargs rm -f