{"id":2512,"date":"2022-03-13T08:37:29","date_gmt":"2022-03-13T00:37:29","guid":{"rendered":"http:\/\/blog.zengqq.com.cn\/?p=2512"},"modified":"2022-03-13T08:37:31","modified_gmt":"2022-03-13T00:37:31","slug":"%e5%8f%91%e9%80%81%e6%9c%80%e6%96%b0%e7%9a%84ip%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/blog.zengqq.com.cn\/?p=2512","title":{"rendered":"\u53d1\u9001\u6700\u65b0\u7684IP\u5730\u5740"},"content":{"rendered":"\n<p><br>Hello all,\u00a0<br>I&#8217;ve been creating various scripts to hone my scripting skills so I thought I&#8217;d share this here. It&#8217;s just a simple bash script to check current ip address and, if it has changed, email the new current ip to specified address. You may need to create a couple empty files if they don&#8217;t yet exist. (e.g. currentip.txt and oldip.txt) I may add in a test to check for existence and create if necessary at a later date.<\/p>\n\n\n\n<p>Note that you will need to change \/PATH\/TO\/SCRIPT to the proper location \u8bf7\u6ce8\u610f\uff0c\u811a\u672c\u4e2d\u7684\/PATH\/TO\/SCRIPT\u8981\u6362\u6210\u4f60\u81ea\u5df1\u5b58\u653e\u7684\u4f4d\u7f6e\uff0c\u6211currentip.txt\u5b58\u5728\/home\/pi\/Documents (\u6216\u8005\u76f4\u63a5\u7528\uff5e\u6307\u5411\u201c\u5f53\u524d\u7528\u6237pi\u7684Documents\u6587\u4ef6\u76ee\u5f55\u4e0b\u5373~\/Documents\/currentip.txt)<br>Code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/bin\/bash\n\nIP=$(curl ipinfo.io\/ip)\nDIFF=$(diff \/PATH\/TO\/SCRIPT\/currentip.txt &lt;(echo \"$IP\"))\nif [ \"$IP\" ]\nthen\n\n    if [ \"$DIFF\" = \"\" ]\n    then\n        :\n    elif [ \"$DIFF\" != \"\" ]\n    then\n        mv \/PATH\/TO\/SCRIPT\/currentip.txt \/PATH\/TO\/SCRIPT\/oldip.txt        \n        echo \"$IP\" &gt;\/PATH\/TO\/SCRIPT\/currentip.txt\n        mail -s \"SUBJECT\" YOUREMAIL@EXAMPLE.COM &lt; \/PATH\/TO\/SCRIPT\/currentip.txt\n    fi\nelse    \n    :   \nfi<\/pre>\n\n\n\n<p>Any questions, edits\/suggestions are welcome<\/p>\n\n\n\n<p>Oh yeah, before you can use the mail function, you have to install a mail utility example \u201dmail\u201d<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Sudo apt-get install mailutils<\/pre>\n\n\n\n<p>it will take some time to install after you type \u201dy\u201d to allow the installation<\/p>\n\n\n\n<p>some original website info for how to mail with CLI is here, it\u2019s not easy to send as you still have to setup something which is not addressed in the tuition <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-binarytides wp-block-embed-binarytides\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"hPBK4J0jgm\"><a href=\"https:\/\/www.binarytides.com\/linux-mail-command-examples\/\">Linux mail command examples &#8211; send mails from command line<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Linux mail command examples &#8211; send mails from command line&#8221; &#8212; BinaryTides\" src=\"https:\/\/www.binarytides.com\/linux-mail-command-examples\/embed\/#?secret=ejM7RwmuSN#?secret=hPBK4J0jgm\" data-secret=\"hPBK4J0jgm\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Hello all,\u00a0I&#8217;ve bee [&#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-2512","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\/2512","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=2512"}],"version-history":[{"count":1,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/2512\/revisions"}],"predecessor-version":[{"id":2514,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/2512\/revisions\/2514"}],"wp:attachment":[{"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zengqq.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}