Recommended: Python Development detection website horse-hanging Program

Source: Internet
Author: User
Tags md5 stdin

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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.