The system administrator usually retrieves the code from the Svn/git, after deploying the site usually first generates the MD5 value of all the files of the site, if the Site page content is tampered with (such as hanging horse), etc., can be compared to the previous generation MD5 value quickly find to go to those files are changed, in order to enable 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 /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 Importos,sys,subprocessdefUpdate (PATH): F= Open (file,'W') forRoot,dirs,filesinchOs.walk (path): forNameinchFiles:line=Os.path.join (Root, name) (Stdin,stderr)= subprocess. Popen (['md5sum', line],stdout=subprocess. PIPE). Communicate () F.write (stdin) f.close ()defCheck (path): F= Open (file,'R') forLineinchF:CHECK_OK="""echo '%s ' | md5sum-c >/dev/null 2>&1"""% Line#Print Check_ok if notSubprocess.call (CHECK_OK, Shell = True) = =0:abnormal=Line.split ()PrintAbnormal[1] F.close ()defUsage ():Print " "Usage:python%s update/home/wwwroot python%s check/home/wwwroot" "%(Sys.argv[0],sys.argv[0]) sys.exit ()ifLen (SYS.ARGV)! = 3: Usage () file='File.key'Model= Sys.argv[1] Path= Sys.argv[2] ifos.path.exists (path) = =False:Print "\033[;31mthe directory or file does not exist\033[0m"sys.exit ()elifModel = ='Update': Update (PATH)elifModel = ='Check': Check (path)Else: Usage ()
Source: https://blog.linuxeye.com/376.html
Detecting web site hangs Ma Cheng (Python)