The system administrator typically retrieves code from the Svn/git, after you deploy the site, you will usually first generate the MD5 value of all the files in the site, if the Site page content has been tampered with (such as hanging horses), and so on, you can generate MD5 value faster than before to find those files were changed, in order to make the system administrator to find the first time Can be combined with tools such as crontab or Nagios.
The program tests are as follows:
# 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
The code is as follows (check_change.py):
#!/usr/bin/env python
Import os,sys,subprocess
Def update (path):
f = open (file, ' W ')
for root,dirs,files in Os.walk ( Path):
for name in Files:
line = Os.path.join (root, name)
(Stdin,stderr) = subprocess. Popen ([' md5sum ', line],stdout=subprocess.) PIPE). Communicate ()
f.write (stdin)
F.close ()
def check (path):
f = open (file, ' R ')
For line in F:
CHECK_OK = "" "Echo '%s ' | Md5sum-c >/dev/null 2>&1 "" "% line
#print CHECK_OK
If not Subprocess.call (CHECK_OK, Shell = True) = = 0:
abnormal = Line.split ()
Print Abnormal[1]
F.close ()
Def Usage ():
print ' '
Usage:python%s Update/home/wwwroot
Python%s check/home/wwwroot
'% (Sys.argv[0],sys.argv[0])
Sys.exit ()
If Len (SYS.ARGV)!= 3:
Usage ()
File = ' File.key '
Model = Sys.argv[1]
Path = sys.argv[2]
If os.path.exists (path) = = False:
Print "\033[;31mthe directory or file does not exist\033[0m"
Sys.exit ()
Elif model = = ' Update ':
Update (PATH)
Elif model = = ' Check ':
Check (PATH)
Else
Usage ()