{"id":115,"date":"2020-04-22T22:22:46","date_gmt":"2020-04-22T14:22:46","guid":{"rendered":"http:\/\/iotch.xyz\/?p=115"},"modified":"2020-04-22T22:22:46","modified_gmt":"2020-04-22T14:22:46","slug":"%e4%bd%bf%e7%94%a8-python-%e5%b0%86%e6%a0%91%e8%8e%93%e6%b4%be%e7%9a%84-ip-%e5%8f%91%e5%88%b0%e9%82%ae%e7%ae%b1","status":"publish","type":"post","link":"https:\/\/www.iotch.xyz\/?p=115","title":{"rendered":"\u4f7f\u7528 Python \u5c06\u6811\u8393\u6d3e\u7684 IP \u53d1\u5230\u90ae\u7bb1"},"content":{"rendered":"\n<figure class=\"wp-block-embed-wordpress wp-block-embed is-type-wp-embed is-provider-\u6811\u8393\u6d3e\u5b9e\u9a8c\u5ba4\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/shumeipai.nxez.com\/2020\/01\/02\/send-email-raspberry-pi-ip-with-python.html\n<\/div><\/figure>\n\n\n\n<p>\u4f7f\u7528 Python \u5c06\u6811\u8393\u6d3e\u7684 IP \u53d1\u5230\u90ae\u7bb1 | \u6811\u8393\u6d3e\u5b9e\u9a8c\u5ba4<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2013\/11\/20131115223615279-0.jpg\" alt=\"20131115223615279-0\" class=\"wp-image-518\"\/><\/figure><\/div>\n\n\n\n<p>\u672c\u6587\u6765\u81ea CDWEN \u7684\u6295\u7a3f\u3002\u7531\u4e8e\u6811\u8393\u6d3e\u7684\u4f7f\u7528\u573a\u666f\u4e2d\u5927\u591a\u6570\u6ca1\u6709\u914d\u5907\u663e\u793a\u5668\uff0c\u90a3\u4e48\u5982\u4f55\u5f97\u77e5\u6811\u8393\u6d3e\u7684 IP \u5730\u5740\u4ee5\u4fbf\u901a\u8fc7 SSH \u5ba2\u6237\u7aef\u8bbf\u95ee\u5462\uff1f<br>\u4e4b\u524d\u6709\u4ecb\u7ecd\u8fc7\u4e00\u79cd\u5c06\u6811\u8393\u6d3e IP \u5730\u5740\u53d1\u9001\u5230\u90ae\u7bb1\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u662f<a href=\"https:\/\/shumeipai.nxez.com\/2014\/03\/18\/let-raspberry-pi-ip-address-is-automatically-reported-to-the-mailbox.html\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >\u901a\u8fc7 mutt \u548c msmtp \u6765\u5b9e\u73b0\u7684<\/a>\u3002\u4e0b\u9762\u5c06\u4ecb\u7ecd\u53e6\u4e00\u79cd\u65b9\u5f0f\uff0c\u901a\u8fc7 Python \u548c\u90ae\u4ef6\u670d\u52a1\u5546\u7684 SMTP \u670d\u52a1\u5668\u6765\u53d1\u9001\u5e26\u6709 IP \u4fe1\u606f\u7684\u90ae\u4ef6\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u7f16\u5199\u4e24\u4e2a\u811a\u672c\uff0cPython \u90ae\u4ef6\u53d1\u9001\u811a\u672c mail.py \u548c\u83b7\u53d6\u6811\u8393\u6d3e IP \u4fe1\u606f\u7684\u811a\u672c ip.sh\u3002<\/p>\n\n\n\n<p>mail.py \u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>123456789101112131415161718192021222324252627282930<\/td><td><code>#!\/usr\/bin\/python <\/code><code>#-*- coding:utf-8 -*-<\/code><code>import<\/code> <code>smtplib <\/code><code>from<\/code> <code>email.mime.text <\/code><code>import<\/code> <code>MIMEText <\/code><code>import<\/code> <code>sys&nbsp; <\/code><code>mail_host <\/code><code>=<\/code> <code>'smtp.exmail.qq.com'<\/code>&nbsp;&nbsp; <code>#smtp\u5730\u5740\u5982\u679c\u4e0d\u77e5\u53ef\u4ee5\u767e\u5ea6\u5982\u201c163\u90ae\u7bb1smtp\u5730\u5740\u201d<\/code><code>mail_user <\/code><code>=<\/code> <code>'\u90ae\u7bb1\u8d26\u53f7'<\/code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <code>#\u6b64\u8d26\u53f7\u5bc6\u7801\u662f\u7528\u6765\u7ed9\u4eba\u53d1\u9001\u90ae\u4ef6\u7684<\/code><code>mail_pass <\/code><code>=<\/code> <code>'\u90ae\u7bb1\u5bc6\u7801'<\/code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <code>#\u6b64\u8d26\u53f7\u5bc6\u7801\u662f\u7528\u6765\u7ed9\u4eba\u53d1\u9001\u90ae\u4ef6\u7684<\/code><code>mail_postfix <\/code><code>=<\/code> <code>'exmail.qq.com'<\/code>&nbsp;&nbsp; <code>#\u90ae\u7bb1\u5730\u5740\uff0csmtp\u5730\u5740\u5220\u53bbsmtp\u5b57\u7b26\u5982\u201c163.com\u201d<\/code>&nbsp;<code>def<\/code> <code>send_mail(to_list,subject,content): <\/code><code>me <\/code><code>=<\/code> <code>mail_user<\/code><code>+<\/code><code>\"&lt;\"<\/code><code>+<\/code><code>mail_user<\/code><code>+<\/code><code>\"@\"<\/code><code>+<\/code><code>mail_postfix<\/code><code>+<\/code><code>\"&gt;\"<\/code><code>msg <\/code><code>=<\/code> <code>MIMEText(content) <\/code><code>msg[<\/code><code>'Subject'<\/code><code>] <\/code><code>=<\/code> <code>subject <\/code><code>msg[<\/code><code>'From'<\/code><code>] <\/code><code>=<\/code> <code>me <\/code><code>msg[<\/code><code>'to'<\/code><code>] <\/code><code>=<\/code> <code>to_list <\/code>&nbsp;<code>try<\/code><code>: <\/code><code>s <\/code><code>=<\/code> <code>smtplib.SMTP_SSL() <\/code><code>s.connect(mail_host) <\/code><code>s.login(mail_user,mail_pass) <\/code><code>s.sendmail(me,to_list,msg.as_string()) <\/code><code>s.close() <\/code><code>return<\/code> <code>True<\/code><code>except<\/code> <code>Exception,e: <\/code><code>print<\/code> <code>str<\/code><code>(e) <\/code><code>return<\/code> <code>False<\/code>&nbsp;<code>if<\/code> <code>__name__ <\/code><code>=<\/code><code>=<\/code> <code>\"__main__\"<\/code><code>: <\/code><code>send_mail(sys.argv[<\/code><code>1<\/code><code>], sys.argv[<\/code><code>2<\/code><code>], sys.argv[<\/code><code>3<\/code><code>])<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>ip.sh \u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>12345678<\/td><td><code>#!\/bin\/bash<\/code><code>sleep<\/code> <code>10<\/code><code>wlan0=`<\/code><code>ifconfig<\/code>&nbsp; <code>wlan0 | <\/code><code>head<\/code> <code>-n2 | <\/code><code>grep<\/code> <code>inet | <\/code><code>awk<\/code> <code>'{print$2}'<\/code><code>`<\/code><code>eth0=`<\/code><code>ifconfig<\/code>&nbsp; <code>eth0 | <\/code><code>head<\/code> <code>-n2 | <\/code><code>grep<\/code> <code>inet | <\/code><code>awk<\/code> <code>'{print$2}'<\/code><code>`<\/code>&nbsp;<code>if<\/code> <code>ping<\/code> <code>-c 2 -W 3 www.baidu.com &amp;&gt; <\/code><code>\/dev\/null<\/code> <code>;<\/code><code>then<\/code><code>\/home\/pi\/mail<\/code><code>.py \u6536\u4ef6\u4eba\u90ae\u7bb1\u5730\u5740\uff08\u4e0d\u53ef\u4e0email.py\u811a\u672c\u4e2d\u8d26\u53f7\u4e00\u81f4\uff09 <\/code><code>\"\u6811\u8393\u6d3e\u5df2\u7ecf\u5f00\u673a\uff0c\u6210\u529f\u83b7\u53d6\u5230ip\u5730\u5740\"<\/code> <code>\"\u65e0\u7ebf\u7f51\u7edc\u5730\u5740\uff1a$wlan0||\u6709\u7ebf\u7f51\u7edc\u5730\u5740\uff1a$eth0\"<\/code><code>fi<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5047\u8bbe\u811a\u672c\u5728 \/home\/pi\/ \u76ee\u5f55\u4e0b\uff0c\u7ed9\u811a\u672c\u8d4b\u4e88\u6267\u884c\u6743\u9650\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>12<\/td><td><code>sudo<\/code> <code>chmod<\/code> <code>+x <\/code><code>\/home\/pi\/ip<\/code><code>.sh<\/code><code>sudo<\/code> <code>chmod<\/code> <code>+x <\/code><code>\/home\/pi\/mail<\/code><code>.py<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5c06\u811a\u672c\u52a0\u5165\u5f00\u673a\u6267\u884c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td><code>sudo<\/code> <code>vim <\/code><code>\/etc\/rc<\/code><code>.<\/code><code>local<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5728 exit 0 \u4e4b\u524d\u52a0\u5165\u4e0b\u9762\u4e00\u884c<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>1<\/td><td><code>sudo<\/code> <code>\/home\/pi\/ip<\/code><code>.sh<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"has-text-color has-vivid-red-color\">\u91cd\u542f\u6811\u8393\u6d3e\uff0c\u5f00\u673a\u540e\u7b49\u5f85\u4e00\u70b9\u65f6\u95f4\uff0c\u6253\u5f00\u4f60\u7684\u90ae\u7bb1\u4f1a\u6536\u5230\u4e00\u4efd\u6811\u8393\u6d3e\u7684 IP \u5730\u5740\u4fe1\u606f\u7684\u90ae\u4ef6\uff0c\u77e5\u9053 IP \u4e4b\u540e\u53ef\u4ee5\u66f4\u65b9\u4fbf SSH \u8fde\u63a5\u5230\u6811\u8393\u6d3e\u64cd\u4f5c\u6216\u8fdb\u884c VNC \u64cd\u4f5c\u3002<strong>\u6587\u7ae0\u6807\u9898\uff1a<\/strong><a href=\"https:\/\/shumeipai.nxez.com\/2020\/01\/02\/send-email-raspberry-pi-ip-with-python.html\" target=\"_blank\"  rel=\"nofollow\" >\u4f7f\u7528 Python \u5c06\u6811\u8393\u6d3e\u7684 IP \u53d1\u5230\u90ae\u7bb1<\/a> - <a href=\"https:\/\/shumeipai.nxez.com\" target=\"_blank\"  rel=\"nofollow\" >\u6811\u8393\u6d3e\u5b9e\u9a8c\u5ba4<\/a><strong>\u56fa\u5b9a\u94fe\u63a5\uff1a<\/strong><a href=\"https:\/\/shumeipai.nxez.com\/2020\/01\/02\/send-email-raspberry-pi-ip-with-python.html\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/shumeipai.nxez.com\/2020\/01\/02\/send-email-raspberry-pi-ip-with-python.html<\/a><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"115\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Python \u5c06\u6811\u8393\u6d3e\u7684 IP \u53d1\u5230\u90ae\u7bb1 | \u6811\u8393\u6d3e\u5b9e\u9a8c\u5ba4 \u672c\u6587\u6765\u81ea CDWEN \u7684\u6295\u7a3f\u3002\u7531\u4e8e\u6811\u8393\u6d3e\u7684\u4f7f\u7528\u573a\u666f\u4e2d\u5927\u591a\u6570\u6ca1\u6709\u914d &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-11"],"_links":{"self":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=115"}],"version-history":[{"count":0,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}