First, the program test
Copy Code code 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
Second, the implementation code is as follows (check_change.py)
Copy Code code as follows:
#!/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 ()