2017-6-10-1 ······开机自动检测并发送IP地址到指定邮箱
来源:互联网 发布:深圳策略一二三网络 编辑:程序博客网 时间:2024/05/22 03:07
2017-6-10 学习日志 (未成功,记录以备后解)
注
所以后来使用了msmtp+mutt+shell脚本的方式成功实现了开机检测IP并发送到指定邮箱。 详情见下一篇文章----2017-6-10-2学习日志
树莓派开机自动检测并发送IP地址到指定邮箱
在不固定的网络环境下(如出差),IP地址是不固定的,每次都用网线连接电脑查看太麻烦,开发LED屏显示IP地址更耗时间,今天突然想到,可以写个脚本,开机自动检测IP发送到我的邮箱,比较方便。
python的网络功能强大,遂使用python编写脚本,参照的网络文章。
参考文章:http://blog.csdn.net/u013151320/article/details/50251959
一. 新建一个python脚本文件
[johnwick@raspberrypi~]$nano send_ip.py
二. 编辑脚本内容
获取python的位置
[johnwick@raspberrypi~]$which python/usr/bin/python
编写代码 (未进行检查(有相当多的错误)没有错误提示的代码请看内容 4)
#!/usr/bin/python#-*-coding:utf-8-*-import socketimport timeimport smtplibimport urllibfrom email.mine.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImage def sendEmail(smtpserver,username,password,sender,receiver,subject,msghtml):msgRoot =MIMEMultipart('reklated')msgRoot['to']=','.join(receiver)msgRoot['Subject']= subjectmsgText = MIMEText(msghtml,'html','utf-8')msgRoot.attach(msgText)smtp = smtplib.SMTP()smtp.connect(smtpserver,"25")smtp.login(username,password)print "yes"smtp.quit()#检查网络连通性def check_network() while True: try: result=urllib.urlopen('http://baidu.com').read() print result print "Network is Ready!" break except Exception , e: print e print "Network is not ready,Sleep 5s..." time.sleep(5) return True#获取 本级制定接口的IP地址 def get_ip_address(): s =socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.connect(("1.1.1.1",80)) ipaddr=s.getsockname()[0] s.close() return ipaddr if __name__ == '__main__' : check_network() ipaddr= get_ip_address() sendEmail("smtp.sohu.com",'你的邮箱帐号','你的邮箱密码','发件人地址',['收件人地址'],'IP Address of Raspberry PI',ipaddr)
期间调试发现,因为复制粘贴,出现了相当多的错误提示
大部分都是因为不正确地使用缩进量。
由于python语法对缩进量的要求极为严格(比如:同级语句缩进量必须相同、禁止空格和tab混用)大部分的操作记录如下:
[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 12 msgRoot =MIMEMultipart('reklated') ^IndentationError: expected an indented block[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 12 msgRoot =MIMEMultipart('reklated') ^IndentationError: expected an indented block[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 12 msgRoot = MIMEMultipart('reklated') ^IndentationError: expected an indented block[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 25 def check_network() ^SyntaxError: invalid syntax[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 21 smtp.quit() ^IndentationError: unexpected indent[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 25 def check_network() ^SyntaxError: invalid syntax[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 25 def check_network() ^SyntaxError: invalid syntax[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 25 def check_network ^SyntaxError: invalid syntax[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 25 def check_network() ^SyntaxError: invalid syntax[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 49 sendEmail("smtp.163.com",'johnwick101101@163.com','333333333333333','weifeng101@163.com',['weifeng101@163.com'],'IP Address of Raspberry PI',ipaddr) ^IndentationError: unindent does not match any outer indentation level[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 49 sendEmail("smtp.163.com",'johnwick101@163.com','3333333333333333','weifeng101@163.com',['weifeng101@163.com'],'IP Address of Raspberry PI',ipaddr) ^IndentationError: unindent does not match any outer indentation level[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.pyTraceback (most recent call last): File "send_ip.py", line 7, in <module> from email.mine.multipart import MIMEMultipartImportError: No module named mine.multipart[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.pyyesTraceback (most recent call last): File "send_ip.py", line 21, in <module> smtp.quit()NameError: name 'smtp' is not defined[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 21 smtp.quit() ^IndentationError: unexpected indent[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py File "send_ip.py", line 21 smtp.quit(): ^SyntaxError: invalid syntax[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py<html><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></html>Network is Ready!Traceback (most recent call last): File "send_ip.py", line 49, in <module> sendEmail("smtp.163.com",'johnwick101@163.com','23333333333','weifeng101@163.com',['weifeng101@163.com'],'IP Address of Raspberry PI',ipaddr) File "send_ip.py", line 19, in sendEmail smtp.login(username,password) File "/usr/lib/python2.7/smtplib.py", line 622, in login raise SMTPAuthenticationError(code, resp)smtplib.SMTPAuthenticationError: (550, 'User has no permission')[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py<html><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></html>Network is Ready!Traceback (most recent call last): File "send_ip.py", line 49, in <module> sendEmail("smtp.163.com",'johnwick101@163.com','23333333333333','weifeng101@163.com',['weifeng101@163.com'],'IP Address of Raspberry PI',ipaddr) File "send_ip.py", line 19, in sendEmail smtp.login(username,password) File "/usr/lib/python2.7/smtplib.py", line 622, in login raise SMTPAuthenticationError(code, resp)smtplib.SMTPAuthenticationError: (535, 'Error: authentication failed')[johnwick@raspberrypi~]$nano send_ip.py[johnwick@raspberrypi~]$python send_ip.py<html><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></html>Network is Ready!yes
没有错误提示的代码
#!/usr/bin/python#-*-coding:utf-8-*-import socketimport timeimport smtplibimport urllibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImagedef sendEmail(smtpserver,username,password,sender,receiver,subject,msghtml): msgRoot = MIMEMultipart('reklated') msgRoot['to']=','.join(receiver) msgRoot['Subject']= subject msgText = MIMEText(msghtml,'html','utf-8') msgRoot.attach(msgText) smtp = smtplib.SMTP() smtp.connect(smtpserver,"25") smtp.login(username,password) print "yes" smtp.quit() #检查网络连通性def check_network(): while True: try: result=urllib.urlopen('http://baidu.com').read() print result print "Network is Ready!" break except Exception , e: print e print "Network is not ready,Sleep 5s..." time.sleep(5) return True#获取 本级制定接口的IP地址def get_ip_address(): s =socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.connect(("0.0.0.0",80)) ipaddr=s.getsockname()[0] s.close() return ipaddrif __name__ == '__main__' : check_network() ipaddr= get_ip_address() sendEmail('smtp.163.com','johnwick101@163.com','2345125241241','weifeng101@163.com',['weifeng101@163.com'],'IP Address of Raspberry PI',ipaddr)
执行情况:
执行后没发送邮件,只进行了网络连通性检测,没有错误提示
[johnwick@raspberrypi~]$python send_ip.py<html><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></html>Network is Ready!yes
细节:
选择发送邮件的邮箱后(我想用johnwick101@163.com作为代理邮箱,通过smtp 服务代理发送,但是网易的安全策略相当严格,使用SMTP服务需要进行验证)加上使用sendmail服务发送邮件有一些相当复杂且严谨的配置。
所以后来使用了msmtp+mutt+shell脚本的方式成功实现了开机检测IP并发送到指定邮箱。
详情见下一篇文章—-2017-6-10-2学习日志
阅读全文
0 0
- 2017-6-10-1 ······开机自动检测并发送IP地址到指定邮箱
- 2017-6-10-2 ······开机自动检测并发送IP地址到指定邮箱
- PI 开机后发送ip地址到指定邮箱
- 工作环境搭建(6) - CentOS7开机自动发送IP地址到指定邮箱
- [Raspberry]开机发送IP到邮箱
- 将ip发送到指定邮箱
- 树莓派自动发送ip到指定邮箱
- 【python】获取本机局域网IP并发送邮件到指定邮箱
- 开机上报树梅派2的IP地址到邮箱
- 树莓派实战2:发送ip地址到自己邮箱
- ddnslib.py - 更新adsl用户外网ip到动态域名网站no-ip.com,并发送新ip到指定邮箱的python脚本
- 开机实现将手机联系人、通话记录、手机号码、手机所在地、发送到指定邮箱里
- Android 使用javaMail jar包发送邮件到指定邮箱,并可以发送图片附件
- 进制·IP地址转换
- JAVA自动生成sql结果到excel,并以附件方式发送到指定邮箱
- 静态页面,表单发送到指定邮箱
- crontab输出内容发送到指定邮箱
- 网页表单内容发送到指定邮箱
- less-mixin(混合)
- 【快捷键】IntelliJ新建一个类 和 生成getter/setter方法的快捷键
- HashMap扩容机制
- Linux学习基础篇(七)
- 第三方支付架构设计之:商户回调通知系统的悲观和乐观策略
- 2017-6-10-1 ······开机自动检测并发送IP地址到指定邮箱
- 算法--Java
- UUIDUtils工具随即生成UUID字符串
- 手机端时间选择器慢input type="date"
- python requests 详细学习content 和text 的区别
- JMeter的安装及处理"No X11"错误
- [LeetCode] Number of Islands
- 445. Add Two Numbers II
- servlet request getHeader("x-forwarded-for") 获取真实IP