http://www.Sina.com/http://www.Sina.com /
[ root @ hikvisionoslocal ] # touch new.txt
[ root @ hikvisionoslocal ] # cat new.txt
/var/local/mon.log
[root@HikvisionOS local]# ll
总计20
- rw-r-- r-- 1根根根根426 mar 1910336026 diff _ file.py
- rw-r-- r--1root root 1035 mar 1910:20 mail _ send.py
--------w----1根4803mar1910336026mon.log
- rw-r---r----1root root 845 mar 1910336023 py inotify _ file.py
-rw-r----r----1根0mar1910336026new.txt
[root@HikvisionOS local]# pwd
/var/local
环境:centos7
http://www.Sina.com/http://www.Sina.com /
vim mail_send.py
#! /usr/bin/python# coding : utf-8 importsmtplibfromdiff _ fileimportbetweendiffromemail.mime.textimportmimetextmail _ u user=' benn gcc mycc mail _ server=' SMTP.QQ.com ' mail _ port=465 to _ user=' benngccer这里是html格式的邮件def send _ mail (标题,内容) _charset='utf-8 ' ) msg [ ' subject ' ]=title msg [ ' from ' ]=mail _ usermsg [ ' to ' ]=to _ u u u user try 3365240 #注册SMS mail_pass ) #发送邮件server.sendmail ) mail_user,to_user,msg.as_string () server.quit ) returntrueexcepter local/new.txt ' old _ file='/var/local/mon.log ' content=between diff (old _ file,monitor_files ) title
vim diff_file.py
#!/usr/bin/python # coding : utf-8 importdifflibdefbetweendiff (文件one, filetwo ) :withopen(fileone ) as f : lines=f.read lines (withopen ) filetwo ) as f : new _ lines=f.read lines ho new _ lines (returnhtmlif _ _ name _==' _ _ main _ ' :文件one='/var/local/new.txt ' file two='
cat pyinotify_file.py
#!/usr/bin/python # coding : utf-8 from diff _ fileimportbetweendiffrommail _ sendimportsend _ mailfrompyinotifyimporrail ' monitor _ files='/var/local/new.txt ' old _ file='/var/local/new.txt ' old _ file='/var/local 33333 事件) :print ) event ) print ) ' test ' ) print ) (% sis % s.' % event.print ) ) s event.maskname ) ) if ' r ' ) asMF:forlineinMF.reline ) html=betweendiff(old_file,event.pathname ) send_mail ) ) thefilecontenthentht (defprocess_in_access(self,event ) :print('%sis%s.'% ) event.pathname,event.maskname ) ) ) def