]> Zhao Yanbai Git Server - acecode.git/commitdiff
add get_lan_ip
authorAceVest <zhaoyanbai@126.com>
Mon, 4 Jul 2016 16:03:37 +0000 (00:03 +0800)
committerAceVest <zhaoyanbai@126.com>
Mon, 4 Jul 2016 16:03:37 +0000 (00:03 +0800)
.gitignore
RaspberryPi/get_lan_ip.py [new file with mode: 0755]

index 6522803f5b0ba1529d2206e846db906a8e12f439..687f5a3b2a1b9f825c5ed392dfc552437e45fbe8 100644 (file)
@@ -6,4 +6,5 @@ ish
 *.DS_Store
 *.swp
 *.ef
+*.conf
 dvwa
diff --git a/RaspberryPi/get_lan_ip.py b/RaspberryPi/get_lan_ip.py
new file mode 100755 (executable)
index 0000000..1cd27b2
--- /dev/null
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# ------------------------------------------------------------------------
+#   File Name: get_lan_ip.py
+#      Author: Zhao Yanbai
+#              2016-07-04 23:14:50 Monday CST
+# Description: none
+# ------------------------------------------------------------------------
+import socket
+import fcntl
+import struct
+import os
+import sys
+import re,urllib2
+import smtplib
+import ConfigParser
+from email.mime.text import MIMEText
+from email.header import Header
+
+def get_ip_address(ifname):
+    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+    return socket.inet_ntoa(fcntl.ioctl(
+        s.fileno(),
+        0x8915,  # SIOCGIFADDR
+        struct.pack('256s', ifname[:15])
+    )[20:24])
+
+def SendMail(title, content) :
+    cnfp = ConfigParser.ConfigParser()
+    cnfp.read('./mail.conf')
+
+    mail_host = cnfp.get('EMAIL', 'MAILHOST')  # 服务器
+    mail_user = cnfp.get('EMAIL', 'MAILUSER')  # 用户名
+    mail_pass = cnfp.get('EMAIL', 'MAILPASS')  # 口令
+    sender    = cnfp.get('EMAIL', 'SENDER')    # 发送方
+    receivers = [cnfp.get('EMAIL', 'MAILTO')]  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
+
+    message = MIMEText(content, 'plain', 'utf-8')
+
+    subject = title + '[' + cnfp.get('EMAIL', 'DEVICE') + ']'
+    message['Subject'] = Header(subject, 'utf-8')
+
+
+    try:
+        smtpObj = smtplib.SMTP_SSL()
+        smtpObj.connect(mail_host, 465)
+        smtpObj.login(mail_user,mail_pass)
+        smtpObj.sendmail(sender, receivers, message.as_string())
+        print "邮件发送成功"
+    except smtplib.SMTPException, e:
+        print e
+        print "Error: 无法发送邮件"
+
+
+if __name__ == "__main__" :
+    lanip = { 'wlan0':'NULL', 'eth0':'NULL', 'lo':'NULL' }
+    for k, v in lanip.items() :
+        try :
+            lanip[k] = get_ip_address(k)
+        except :
+            continue
+
+    SendMail("LanIp", str(lanip))