I. program testing
Copy codeThe Code is 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 # generate the site's md5 Value
# Echo ''>/data/www/sitemap.html # Test File clearing
# Rm-rf/data/www/sitemap. xml # Test File Deletion
# Python check_change.py check/data/www # search for files tampered
/Data/www/sitemap. xml
/Data/www/sitemap.html
II. The implementation code is as follows (check_change.py)
Copy codeThe Code is 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 [; 31 mThe directory or file does not exist \ 033 [0 m"
Sys. exit ()
Elif model = 'update ':
Update (path)
Elif model = 'check ':
Check (path)
Else:
Usage ()