Using Python and MD5 to implement the web-linked horse detection program

Source: Internet
Author: User
first, the program test
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 #生成站点的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 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[;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.