执行sh文件_对 Centos7 重要系统文件定时校验 md5 码,并发信告警

写在前面:

对于生产环境系统来说,系统文件被动了手脚之后,排查起来很花精力,我们今天来看其中的一个小方法:对系统重要目录文件,定期计算校验 MD5 码,初步了解文件变化情况,并可把校验结果以 E-mail 的方式,提交给指定邮箱!

6193ae459bee6f429c9d9201667d4a2b.png

一,导出重要文件列表:

  • # ls /etc/{passwd,shadow,group} > important.file
  • # find /usr/sbin /usr/bin -perm /6000 >>important.file

二,计算初始 MD5值:

  • # vim md5.sh
#!/bin/bashfor filename in $(cat important.file)do md5sum $filename >> finger1.filedone
  • # sh md5.sh
  • # 输出 finger1.file
  • # chattr +i finger1.file

三,编辑最新校验代码:

  • finger1.file 和 finger_new.file 进行比较,决定是否发送邮件!
  • # vim md5.checkfile.sh
[ -f finger_new.file ] && rm finger_new.filefor filename in $(cat important.file)do md5sum $filename >> finger_new.filedone testing=$(diff finger1.file finger_new.file) if [ "$testing" != "" ] ; then diff finger1.file finger_new.file | mail -s 'finger trouble.' XXX@XX.comfi if [ "$testing" = "" ] ; then diff finger1.file finger_new.file | mail -s 'Well done! -ALL are OK!' XXX@XX.comfi

四,运行测试,邮件到达:

  • # sh ./md5.checkfile.sh
  • Null message body; hope that's ok
44efe4fe72e06e2de83135dbe00b4fd7.png
1336cf223ac7c53b1fd2d6f8343c3bdd.png

五,设置定时执行,例如N小时执行一次!

  • # vim /etc/crontab
  • 0 */6 * * * cd /; sh md5.checkfile.sh
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值