]> Zhao Yanbai Git Server - acecode.git/commitdiff
Add python tools
authorroot <zhaoyanbai@126.com>
Thu, 30 Oct 2014 06:46:15 +0000 (06:46 +0000)
committerroot <zhaoyanbai@126.com>
Thu, 30 Oct 2014 06:46:15 +0000 (06:46 +0000)
learn/python/pyinotify.py [new file with mode: 0755]
tools/GetWanIP.py
tools/python/AceGlobalConfSample.conf [new file with mode: 0644]
tools/python/GetWanIP.py [new file with mode: 0755]
tools/python/__init__.py [new file with mode: 0755]
tools/python/config.py [new file with mode: 0755]
tools/python/packges [new file with mode: 0644]
tools/python/utils.py [new file with mode: 0755]

diff --git a/learn/python/pyinotify.py b/learn/python/pyinotify.py
new file mode 100755 (executable)
index 0000000..30c8506
--- /dev/null
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# ------------------------------------------------------------------------
+#   File Name: pyinotify.py
+#      Author: Zhao Yanbai
+#              Thu Oct 30 05:56:56 2014
+# Description: none
+# ------------------------------------------------------------------------
+
+from pyinotify import WatchManager, Notifier, ProcessEvent, EventsCodes
+
+class EventHandler(ProcessEvent) :
+    def process_IN_MODIFY(self, event) :
+        print("Modify file: ", event.path)
+
+
+def Monitor(path) :
+    wm = WatchManager()
+    mask = EventsCodes.FLAG_COLLECTIONS['OP_FLAGS']['IN_MODIFY']
+    print "mask ", mask
+    
+
+    notifier = Notifier(wm, EventHandler())
+    wm.add_watch(path, mask, rec=True)
+    print 'Now Start to Watching ', path
+    while True :
+        try :
+            notifier.process_events()
+            if notifier.check_events() :
+                notifier.read_events()
+
+        except KeyboardInterrupt :
+            notifier.stop()
+            break 
+
+
+if __name__ == "__main__" :
+    Monitor('./a.txt')
+
+
+
+
+
index f43894ace0471735821c097841dcd245fddc9e2a..a6629fc924bde3f377b61c0e43ea739d7a420ef0 100755 (executable)
@@ -96,9 +96,8 @@ if __name__ == '__main__':
         print s
         if send_mail(mailto_list,"New WanIP", s):  
             print "发送成功"  
+            fd = open(TMP_FILE_PATH, 'w+')
+            fd.write(WanIP+'\n')
+            fd.close()
         else:  
             print "发送失败"  
-
-        fd = open(TMP_FILE_PATH, 'w+')
-        fd.write(WanIP+'\n')
-        fd.close()
diff --git a/tools/python/AceGlobalConfSample.conf b/tools/python/AceGlobalConfSample.conf
new file mode 100644 (file)
index 0000000..f9aed80
--- /dev/null
@@ -0,0 +1,7 @@
+[EMAIL]
+MAILTO  = xxx@xx.com
+MAILHOST= smtp.xx.com
+MAILUSER= xxx
+USERNICK= xx 
+MAILPASS= *****
+MAILPOSTFIX= xx.com
diff --git a/tools/python/GetWanIP.py b/tools/python/GetWanIP.py
new file mode 100755 (executable)
index 0000000..fe2c1fe
--- /dev/null
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# ------------------------------------------------------------------------
+#   File Name: GetWanIP.py
+#      Author: Zhao Yanbai
+#              Thu Oct 30 04:34:37 2014
+# Description: none
+# ------------------------------------------------------------------------
+import os
+import re,urllib2
+import smtplib  
+import ConfigParser
+import config
+from email.mime.text import MIMEText  
+from utils import * 
+
+'''
+    CONFIG FILE SAMPLE
+[EMAIL]
+MAILTO  = xxx@xx.com
+MAILHOST= smtp.xx.com
+MAILUSER= xxxxx
+USERNICK= xxx
+MAILPASS= *****
+MAILPOSTFIX= xx.com
+'''
+'''
+    CONTAB
+* * * * * root cd /xxx/xxx/xxx/ && python GetWanIP.py
+'''
+
+TMP_FILE_PATH = '/tmp/GetWanIP.txt'
+log = CreateLogger(config.ACE_GLOBAL_LOG_PATH)
+  
+mailto_list=[] 
+mail_host=""        #设置服务器
+mail_user=""        #用户名
+user_nick=""
+mail_pass=""        #口令 
+mail_postfix=""     #发件箱的后缀
+
+class GetWanIP:
+    def GetIP(self):
+        try:
+            log.info('Try ip.qq.com')
+            WanIP = self.Visit('http://ip.qq.com')
+        except:
+            s = 'Failed to Get WanIP!!!'
+            WanIP = s
+            log.error(s)
+        return WanIP
+
+    def Visit(self,url):
+        opener = urllib2.urlopen(url)
+        if url == opener.geturl():
+            str = opener.read()
+        return re.search('\d+\.\d+\.\d+\.\d+',str).group(0)
+
+
+def send_mail(to_list,sub,content):                                 #to_list:收件人;sub:主题;content:邮件内容
+    me=user_nick+"<"+mail_user+"@"+mail_postfix+">"                 #这里的hello可以任意设置,收到信后,将按照设置显示
+    msg = MIMEText(content,_subtype='html',_charset='gb2312')       #创建一个实例,这里设置为html格式邮件
+    msg['Subject'] = sub                #设置主题
+    msg['From'] = me  
+    msg['To'] = ";".join(to_list)  
+    try:  
+        s = smtplib.SMTP()  
+        s.connect(mail_host)                        #连接smtp服务器
+        s.login(mail_user,mail_pass)                #登陆服务器
+        s.sendmail(me, to_list, msg.as_string())    #发送邮件
+        s.close()  
+        return True  
+    except Exception, e:  
+        log.error(str(e))
+        return False  
+if __name__ == '__main__':  
+    try :
+        fd = open(TMP_FILE_PATH)
+        OldWanIP = fd.readline().strip()
+        fd.close()
+    except :
+        OldWanIP = '0.0.0.0'
+
+    getWanIP = GetWanIP()
+    WanIP = getWanIP.GetIP()
+
+    cnfp = ConfigParser.ConfigParser()
+    cnfp.read(config.ACE_GLOBAL_CONF_PATH)
+    mailto_list.append(cnfp.get('EMAIL', 'MAILTO'))
+    mail_host = cnfp.get('EMAIL', 'MAILHOST')
+    mail_user = cnfp.get('EMAIL', 'MAILUSER')
+    user_nick = cnfp.get('EMAIL', 'USERNICK')
+    mail_pass = cnfp.get('EMAIL', 'MAILPASS')
+    mail_postfix = cnfp.get('EMAIL', 'MAILPOSTFIX')
+
+    if OldWanIP != WanIP :
+        s = 'WanIP has Changed From ' + OldWanIP + ' To ' + WanIP
+        log.info(s)
+        if send_mail(mailto_list,"New WanIP", s):  
+            log.info('发送成功')
+            fd = open(TMP_FILE_PATH, 'w+')
+            fd.write(WanIP+'\n')
+            fd.close()
+        else:  
+            log.error('发送失败')
diff --git a/tools/python/__init__.py b/tools/python/__init__.py
new file mode 100755 (executable)
index 0000000..eca95de
--- /dev/null
@@ -0,0 +1,8 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# ------------------------------------------------------------------------
+#   File Name: __init__.py
+#      Author: Zhao Yanbai
+#              Thu Oct 30 06:21:42 2014
+# Description: none
+# ------------------------------------------------------------------------
diff --git a/tools/python/config.py b/tools/python/config.py
new file mode 100755 (executable)
index 0000000..30ad717
--- /dev/null
@@ -0,0 +1,12 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# ------------------------------------------------------------------------
+#   File Name: config.py
+#      Author: Zhao Yanbai
+#              Thu Oct 30 06:37:52 2014
+# Description: none
+# ------------------------------------------------------------------------
+
+
+ACE_GLOBAL_CONF_PATH = '/etc/AceGlobal.conf'
+ACE_GLOBAL_LOG_PATH  = '/var/log/AceGlobal.log'
diff --git a/tools/python/packges b/tools/python/packges
new file mode 100644 (file)
index 0000000..0d0314f
--- /dev/null
@@ -0,0 +1,2 @@
+pyinotify
+https://github.com/seb-m/pyinotify/archive/0.9.4.tar.gz
diff --git a/tools/python/utils.py b/tools/python/utils.py
new file mode 100755 (executable)
index 0000000..b55788a
--- /dev/null
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# ------------------------------------------------------------------------
+#   File Name: utils.py
+#      Author: Zhao Yanbai
+#              Thu Oct 30 06:33:24 2014
+# Description: none
+# ------------------------------------------------------------------------
+
+import logging
+
+def CreateLogger(logpath, level=logging.DEBUG)  :
+    logger  = logging.getLogger(logpath)
+    fmtr    = logging.Formatter('%(levelname)s\t%(asctime)s: %(message)s')
+
+    fileHdlr    = logging.FileHandler(logpath)
+    fileHdlr.setFormatter(fmtr)
+    fileHdlr.setLevel(level)
+
+    streamHdlr  = logging.StreamHandler()
+    streamHdlr.setFormatter(fmtr)
+    streamHdlr.setLevel(level)
+
+    logger.addHandler(fileHdlr)
+    logger.addHandler(streamHdlr)
+    logger.setLevel(level)
+
+    return logger
+