Deprecated: Function create_function() is deprecated in /www/wwwroot/zhouzexin.cn/usr/plugins/AntiSpam/Plugin.php on line 131
给Tomato dualwan配置邮件提醒功能 - zhouZExin 's Blog
技术 创建于2012年12月6日 [580]
给Tomato dualwan配置邮件提醒功能

很简单,只需要配置一个sendmail的py就可以了。在路由器异常重启而动态域名无法解析到路由器的时候,这个邮件就显得十分重要。

 

直接复制以下代码,保存为sendmail.py文件

#!/usr/bin/env python3
#coding: utf-8
#writer: zhouzexin.cn<admin@zhouzexin.cn>
#2012-12-06 23:06

import smtplib
import os
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from time import sleep
import sys
import time
from string import Template

sender = '[color=Red]xxx[/color]@gmail.com' #这里是用来对外发邮件的邮箱
receiver = 'xxx@qq.com' #这里写需要接收通知的email地址,中间用逗号分隔一下
smtpserver = 'smtp.gmail.com'
username = 'xxx'
ipaddr=os.popen("nvram get wan_ipaddr").read()
password = 'password'  #这里写sender的邮箱密码
receiver = receiver.replace(' ', '').split(',')

#receiver = sys.argv[1].replace(' ', '').split(',')
newip = ipaddr
routername = '路由器名称'
subject = routername + '外网新ip:'+newip

print receiver, newip

msg = MIMEMultipart("alternatvie")

msg['Subject'] = subject
msg['From'] = r"%s %s" % (routername+"路由器", sender)
msg['To'] =  ', '.join(receiver) #很重要,不然不能群发

strdt = time.strftime('%Y-%m-%d %X',time.localtime()) #time,localtime()返回以本地时间的所有参数为元素的元组
bodyfmtcon={"dt":strdt, "newip":newip}
bodyfmt = Template("我已于<font color=blue>$dt</font> 重新连接了互联网,新的ip是:<font color=red>$newip</font>.<p>")

body = "您好!<p>";
body += bodyfmt.substitute(bodyfmtcon)
body += "祝好!<p>";
body += "<i>我是会发邮件的路由器 :)</i>";

html_part = MIMEText(body, 'html')
msg.attach(html_part) #绑定到message里

smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username, password)

print('start send mail...')
smtp.sendmail(sender, receiver, msg.as_string())
sleep(5)
smtp.quit()
print('finish send mail !')

 

然后可以telnet进路由器,运行一下测试:

python2.6 /opt/mail/sendmail.py &

 

基于python2.6运行,所以前提是要安装python:

ipkg install python26

 

妈妈再也不用担心我的路由因停电而重启了!

标签:dualwan, python, senmail
这已经是最后一篇文章
斯巴达后的路由器配置Goagent 2.1.9方案
这已经是最新一篇文章
记录「VPS搬家」需要注意的详细事项
添加新评论
登录管理
︿