python 监控文件变化
2014-06-12 15:44
393 查看
#!/usr/bin/env python # encoding: utf-8 import time,os import pyinotify import smtplib from email.mime.text import MIMEText mailto_list=["695001606@qq.com"] mail_host="smtp.163.com" mail_user="cs@163.com" mail_passwd="**********" file = ['/usr/sbin/sshd','/var/log/syslog','/var/log/lastlog','/var/log/wtmp','/etc/passwd','/etc/shadow'] file1 = ['/usr/sbin/sshd','/etc/passwd','/etc/shadow'] def mail_send(to_list,sub,content): msg = MIMEText(content,_charset='utf-8') msg['Subject'] = sub msg['From'] = mail_user msg['To'] = ";".join(to_list) try: send_smtp = smtplib.SMTP() send_smtp.connect(mail_host) send_smtp.login(mail_user,mail_passwd) send_smtp.sendmail(mail_user,to_list,msg.as_string()) send_smtp.close() return True except Exception,e: print str(e) return False class handler(pyinotify.ProcessEvent): def process_IN_ATTRIB(self,event): if event.pathname in file: mail_send(mailto_list,'文件权限被修改',event.pathname) def process_IN_DELETE(self,event): if event.pathname in file: mail_send(mailto_list,"卧槽,文件被删除了",event.pathname) else: pass def process_IN_MODIFY(self,event): if event.pathname in file1: mail_send(mailto_list,"卧槽,文件被写东西了",event.pathname) else: pass def process_IN_MOVED_TO(self,event): if event.pathname in file: print(event.pathname) mail_send(mailto_list,"卧槽,文件被覆盖了",event.pathname) else: pass def process_IN_MOVED_FROM(self,event): if event.pathname in file: print(event.pathname) mail_send(mailto_list,"卧槽,文件被移走了",event.pathname) else: pass def main(): pathlist = ['/usr/sbin','/var/log','/etc'] wm = pyinotify.WatchManager() wm.add_watch(pathlist,pyinotify.ALL_EVENTS,rec=True) en =handler() notifier= pyinotify.Notifier(wm,en) notifier.loop() if __name__=='__main__': main()要安装pyinotify模块
相关文章推荐
- python监控文件或目录变化
- Python监控文件变化:watchdog
- Python for Windows——监控Windows某个目录下文件的变化
- python中文件变化监控示例(watchdog)
- 使用Python监控文件内容变化代码实例
- Python监控Windows下的文件变化
- Python监控文件变化:watchdog
- python监控文件或目录变化
- 【Python】监控文件内容增删改操作变化
- Python监控Windows下的文件变化
- python中文件变化监控-watchdog
- python 监控文件变化
- Python监控目录和文件变化
- android SD卡文件变化监控
- 用python监视文件变化,定时更新修改过的文件。-- 逆水寒
- 利用python的pyinotify模块监控文件夹和文件的变动
- 监控目录文件变化的ReadDirectoryChangesW 函数学习总结
- java 实现监控某目录文件变化
- visual studio extension: 怎样监控文件的变化
- java监控文件夹内文件变化并且进行文件同步bigfilesync