{"id":1361,"date":"2020-11-26T10:08:52","date_gmt":"2020-11-26T02:08:52","guid":{"rendered":"http:\/\/blog.zengqq.com.cn\/?p=1361"},"modified":"2020-12-05T12:36:15","modified_gmt":"2020-12-05T04:36:15","slug":"gmail%e6%a0%91%e8%8e%93%e6%b4%be%e7%9a%84%e5%85%ac%e7%bd%91ip","status":"publish","type":"post","link":"https:\/\/blog.zengqq.com.cn\/?p=1361","title":{"rendered":"Gmail\u6811\u8393\u6d3e\u7684\u516c\u7f51IP"},"content":{"rendered":"\n<p>I&#8217;m sure this has been done (and posted) before, but here goes anyway:<\/p>\n\n\n\n<p>I wrote this little shell script and thought I&#8217;d share it since it might come in handy to others as well. Simply put, it e-mails the external IP address to you if it has changed since the last time the script was run. I&#8217;ve only tried this with Gmail, but you&#8217;re free to modify the script to suit your needs.<\/p>\n\n\n\n<p>How to use:<\/p>\n\n\n\n<p>\u5148\u5b89\u88c5\u4e2assmtp\u3002First, you must update your package list and install mailutils and ssmtp:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt-get update\nsudo apt-get install mailutils ssmtp<\/pre>\n\n\n\n<p><br>\u65b0\u5b89\u88c5\u7684ssmtp\u7684\u914d\u7f6e\u6587\u4ef6\u5728\/etc\/ssmtp\u4e0b\uff0cThen, open \/etc\/ssmtp\/ssmtp.conf in a text editor (I used Vim) \uff0c\u7528vim\u6216\u8005nano\u7f16\u8f91\uff1a(\u82f1\u6587\u5168\u6587\u5728\u8fd9\u91cc\uff0c\u4f7f\u7528gmail\uff0c\u4e3a\u4e86\u9002\u5e94\u56fd\u5185\u9700\u8981\uff0c\u6211\u6539\u4e3a126\u7684\u7f51\u7ad9)<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo vim \/etc\/ssmtp\/ssmtp.conf    \/\/\u540e\u6765\u624d\u77e5\u9053ssmtp\u5df2\u7ecf\u4e0d\u884c\u4e86<\/pre>\n\n\n\n<p>\u914d\u7f6emailhub\u544a\u77e5\u90ae\u4ef6\u53bb\u54ea\u91cc\uff0cEdit the mailhub line so that it looks like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mailhub=smtp.gmail.com:587 \/\/\u539f\u6587\u662fgmail\u7684smtp\nmailhub=smtp.126.com \/\/\u4e0d\u77e5\u9053126\u7684\u7aef\u53e3\u662f25\uff0c\u8fd9\u91cc\u4e0d\u653e\uff0c\u770b\u80fd\u4e0d\u80fd\u884c<\/pre>\n\n\n\n<p>and add these to the end of the file (replace the AuthUser and AuthPass values with your gmail address and password):<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">AuthUser=siemen@gmail.com\nAuthPass=wodemima\nUseSTARTTLS=YES\n\nAuthUser=iking@126.com\nAuthPass=wodemima\nUseSTARTTLS=YES<\/pre>\n\n\n\n<p>Save the file. Then open \/etc\/ssmtp\/revaliases in a text editor&#8230;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo vim \/etc\/ssmtp\/revaliases<\/pre>\n\n\n\n<p>&#8230;and add this to the bottom of the file, again replacing &#8220;yourgmailaddress&#8221; with your own Gmail login:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">root:siemen.zem@gmail.com:smtp.gmail.com:587\n\nroot:iking@126.com:smtp.126.com:25 \/\/\u5bf9126\u7684\u90ae\u7bb1\u8fdb\u884croot\u7684\u914d\u7f6e\npi:iking@126.com:smtp.126.com:25 \/\/\u5bf9pi\u8fdb\u884c\u914d\u7f6e<\/pre>\n\n\n\n<p id=\"block-c1c073e3-f8b3-4543-a6d0-164f31b19af6\">All that is needed now is the script itself.<\/p>\n\n\n\n<p>A quick explanation on how it works: When you run it for the first time, it retrieves your external IP address and stores it in a file called &#8220;ip.txt&#8221;. This file will be created in the same folder as the script. Then, if the script is run again, it will load the previous IP address from the file, and will also check your current address. If these two match, i.e. the address has not changed, the script will just exit. If they do not match (the address has changed), the new IP address will be written to the text file (overwriting the old one) and emailed to you. Then the script will exit.<\/p>\n\n\n\n<p>Put the subject of the message between the quotes in SUBJ, and the email address of the recipient between the quotes in EMAIL.<\/p>\n\n\n\n<p>Code:&nbsp;<a href=\"https:\/\/www.raspberrypi.org\/forums\/viewtopic.php?t=65010#\">Select all<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/sh\n\nSUBJ=\"RaspiUSB3_163_PubIP\"\nEMAIL=\"Mygmail@gmail.com\"\n\nip1=\"\"\nip2=\"\"\n\nread ip1 &lt; ip.txt\nip2=$(wget -qO- ifconfig.me\/ip)\n\nif [ \"$ip1\" = \"$ip2\" ]\nthen\n  exit\nelse\n  echo \"$ip2\" > ip.txt\n  echo \"$ip2\" | mail -s $SUBJ $EMAIL\n  exit\nfi<\/pre>\n\n\n\n<p>Save the file. You can test it by running &#8220;sh *scriptnamehere*.sh&#8221;. It will show an error on the first start as it is trying to load data from a file that doesn&#8217;t exist. You can safely ignore this. You should receive an e-mail containing your external IP address. The script has also created the &#8220;ip.txt&#8221; file. If you open it, you can see that it contains your &#8220;old&#8221; IP address.<\/p>\n\n\n\n<p>Now, the part where we make it run automatically. We&#8217;re going to use Cron, which is a program you can use to schedule things. This should be installed by default.<\/p>\n\n\n\n<p>First, open the Cron configuration file:<br>sudo crontab -eThen add this line at the bottom:<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">0,15,30,45 * * * * sh *full path to script here*\/*scriptnamehere*.sh &amp;>\/dev\/null<\/pre>\n\n\n\n<p>I will not go into detail on what the different values do &#8211; there are plenty of tutorials and manuals online. The above line runs the script every 15 minutes, 24\/7. It will only send the email message if the IP address has changed. As for the &amp;&gt;\/dev\/null part at the end &#8211; don&#8217;t ask me what it is or how it works. All I know is that it stops Cron from sending useless error reports and other things to your email&nbsp;&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.raspberrypi.org\/forums\/images\/smilies\/icon_lol.gif\" alt=\":lol:\" title=\"Laughing\"\/><\/figure>\n\n\n\n<p>That is all. This should be useful in case you regularly connect to your RasPi from outside your local network. Feel free to post here if you have any questions and I can at least try to help&nbsp;&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.raspberrypi.org\/forums\/images\/smilies\/icon_e_wink.gif\" alt=\";)\" title=\"Wink\"\/><\/figure>\n\n\n\n<p>\u8fd8\u662f\u9047\u5230\u4e00\u5927\u5806\u95ee\u9898\uff0c\u51c6\u5907\u770b\u8fd9\u4e2a<\/p>\n\n\n\n<p><a href=\"http:\/\/xiaqunfeng.cc\/2017\/01\/11\/ubuntu-email-send\/\">http:\/\/xiaqunfeng.cc\/2017\/01\/11\/ubuntu-email-send\/<\/a><\/p>\n\n\n\n<p>\u5b9e\u9645\u64cd\u4f5c\uff0c\u6211\u628ash\u6587\u4ef6\u7a0d\u5fae\u4fee\u6539\u4e86\u4e00\u4e0b\uff0c\u56e0\u4e3assmtp\u5df2\u7ecf\u8fc7\u65f6\u4e86\uff0c\u6211\u7528\u7684\u662fmutt\u7ba1\u7406\u4e0b\u7684msmtp\u53d1\u9001\u90ae\u4ef6<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SUBJ=\"YourNewIPis:\"\nEMAIL=\"zeng.qq@outlook.com\"\n\nip1=\"\"\nip2=\"\"\n\nread ip1 &lt;\/home\/pi\/ip.txt\nip2=$(wget -qO- ifconfig.me\/ip)\n\nif [ \"$ip1\" = \"$ip2\" ]\nthen\n          exit\n  else\n            echo \"$ip2\" >\/home\/pi\/ip.txt\n#echo \u201c$ip2\u201d | mutt -s \u201cThe New IP here\u201d siemen.zem@gmail.com\n                echo \"you get a new IP\" | mutt -s \"New IP address\" siemen.zem@$\nexit\n                fi<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m sure this has b [&#8230;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89],"tags":[],"class_list":["post-1361","post","type-post","status-publish","format-standard","hentry","category-live"],"_links":{"self":[{"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1361"}],"version-history":[{"count":13,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1361\/revisions"}],"predecessor-version":[{"id":1417,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1361\/revisions\/1417"}],"wp:attachment":[{"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}