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

二. 编辑脚本内容

  1. 获取python的位置

    [johnwick@raspberrypi~]$which python/usr/bin/python  
    1. 编写代码 (未进行检查(有相当多的错误)没有错误提示的代码请看内容 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混用)

    1. 大部分的操作记录如下:

      [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
    2. 没有错误提示的代码

      #!/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)
    3. 执行情况:

    执行后没发送邮件,只进行了网络连通性检测,没有错误提示

    [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
原创粉丝点击