系统管理员通常从svn/git获取代码,并在展开站点时首先为该站点上的所有文件生成MD5值。 联机后,如果网站页面的内容被篡改,可以与以前生成的MD5值进行比较,例如被马挂住,以快速查找这些文件的更改。 可以组合crontab和nagios等工具,以便系统管理员首先发现它们。
程序的测试如下。
# python check_change.py
usage : python check _ change.py update/home/wwwroot
python check _ change.py check/home/wwwroot
# python check _ change.py update/data/www #生成网站的md5值
# echo ' ' /data/www/sitemap.html #测试清除文件
# rm -rf /data/www/sitemap.xml #测试删除文件
# python check _ change.py check/data/www #检查那些文件是否已被篡改
/data/www/sitemap.xml
/data/www/sitemap.html
代码如下。 (check_change.py ) :
#! /usr/bin/env python
导入操作系统、sys、subprocess
def更新(path ) :
f=打开(文件,' w ' ) ) )。
for root,dirs,filesinOS.walk(path ) :
for name in files:
line=OS.path.join (根,名称) ) ) ) ) )。
(stdin,stderr )=subprocess.popen(['mD5sum ',line],stdout=subprocess.PIPE ).communicate ) )。
f .写入(stdin )
f.close () )
defcheck(path ) :
f=打开(文件,' r ' ) ) ) )。
for line in f:
check _ ok=' ' echo ' % s '|m D5 sum-c/dev/null 21 ' ' % line
#打印检查_ ok
ifnotsubprocess.call(check_ok,shell=True )==0:
abnormal=line.split (
打印协议[1]
f.close () )
defusage(:
打印' ' ' '
usage : python % s update/home/wwwroot
python %s check /home/wwwroot
“”% (sys.argv[0],sys.argv[0] )
sys.exit () )
iflen(sys.Argv )!=3:
使用() )
file='file.key '
model=sys.argv[1]
path=sys.argv[2]
IFOS.path.exists(path )==False:
print ' 33[; 31 mthedirectoryorfiledoesnotexist 33 [ 0m ]
sys.exit () )
elif model=='update':
是pdate(path )
elif model=='check':
是检查(path )
else:
使用() )
译文: http://blog.Linux eye.com/376.html