サイトの簡易変更チェック
提供:MDWiki
コード
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# file name : /www/scripts/check.py
# compatibility : Python 3.1.x / UTF-8 [LF]
# description : check availability at my best pro
# copyright : Copyright(C)2012 by Takayuki KAWAMOTO
# date : managed since [2012-02-20 17.42 (JST: GMT+0900) @404]
# Git revision :
# -----------------------------------------------------------------------------
from os.path import exists
import urllib.request
import hashlib
import smtplib
import sys
mbps = { 'sanin': '0', 'osaka': '0', 'tokyo': '0', 'kyoto': '0', 'kanagawa': '0',
'ehime': '0', 'yamanashi': '0', 'miyagi': '0', 'okayama': '0', 'kobe': '0',
'toyama': '0', 'hiroshima': '0', 'shizuoka': '0' }
path = '/www/scripts/'
me = 'security@**********.co.jp'
you = 'project_best@**********.co.jp'
for mbp, value in mbps.items():
response = urllib.request.urlopen( 'http://mbp-' + mbp + '.com/information/about.html' )
content = response.read()
hash = hashlib.sha256()
hash.update( content )
digest = hash.hexdigest()
if not exists( path + 'logs/mbp-' + mbp + '.file' ):
out = open( path + 'logs/mbp-' + mbp + '.file', "wt" )
out.write( digest )
out.close()
else:
old = open( path + 'logs/mbp-' + mbp + '.file', "rt" )
oldhash = old.read()
oldhash = oldhash.strip()
old.close()
if digest != oldhash:
mbps[ mbp ] = 'mbp-' + mbp + '.com was changed' + "\n"
out = open( path + 'logs/mbp-' + mbp + '.file', "wt" )
out.write( digest )
out.close()
mbpcount = list( mbps.values() )
if mbpcount.count( '0' ) != len( mbpcount ):
message = """From: Information Security Office <security@**********.co.jp>
Subject: [HASH ALERT] some hashes are changed
To: <project_best@**********.co.jp>
"""
for mbp, value in mbps.items():
if value != '0':
message += 'mbp-' + mbp + '.com was changed.' + "\n"
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail( me, you, message )