From: AceVest Date: Mon, 4 Jul 2016 16:03:37 +0000 (+0800) Subject: add get_lan_ip X-Git-Url: http://zhaoyanbai.com/repos/man.dnssec-importkey.html?a=commitdiff_plain;h=c9a2dd3deac0c40294a6024a7432dc949a8f9883;p=acecode.git add get_lan_ip --- diff --git a/.gitignore b/.gitignore index 6522803..687f5a3 100644 --- a/.gitignore +++ b/.gitignore @@ -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 index 0000000..1cd27b2 --- /dev/null +++ b/RaspberryPi/get_lan_ip.py @@ -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))