{"id":54,"date":"2014-11-17T22:32:48","date_gmt":"2014-11-18T06:32:48","guid":{"rendered":"http:\/\/162.243.52.58\/wifi\/?p=54"},"modified":"2014-11-25T00:07:44","modified_gmt":"2014-11-25T08:07:44","slug":"email-ip-address-on-login","status":"publish","type":"post","link":"https:\/\/www.skifactz.com\/wifi\/?p=54","title":{"rendered":"Email IP Address on Login"},"content":{"rendered":"<h5><span style=\"font-family: Verdana, Arial, Helvetica, sans-serif;\">Automatically Email WAN IP Address at Login &#8211; Linux<\/span><\/h5>\n<p>Note that after this post was written whatsmyip.com has stopped providing support for IP detection automation. You may consider using\u00a0icanhazip.com instead.<\/p>\n<p>While I can only take a partial credit for this bash script which emails the WAN IP address at login, I expanded it and modified it for use with Gmail. The basic idea is that your Linux machine retrieves the internet IP address from <a href=\"http:\/\/www.whatsmyip.org\" target=\"_blank\">www.whatsmyip.org<\/a> and sends it out to an email recipient.<\/p>\n<p>Why is this useful in the first place? My mom lives abroad. She uses Ubuntu and wants me to get inside of her computer to do occasional software updates or fixes. Because her provider assigns dynamic IP addresses to the customers, this script informs me of the IP address changes so I can log into her computer via SSH.<\/p>\n<p>Other uses may include detection of unauthorized use, GPS-free geo tracking, etc.<\/p>\n<p>The script gets the IP address from a third party service, compares it to the previous known address stored in a file called ipaddress, and if different, it emails it to the recipient.<\/p>\n<pre>#!\/bin\/bash\r\n# Emails current IP address to a recipient. Requires package sendEmail with SSL and TLS.\r\n#Last known IP address\r\nIPFILE=\/tmp\/ipaddress\r\n#Allow 60 seconds for the Internet connection to become active\r\nsleep 60\r\n#Retrieve and assign WAN IP address to CURRENT_IP\r\nCURRENT_IP=$(wget -q -O - http:\/\/automation.whatismyip.com\/n09230945.asp)\r\nDATUM=\"$(date)\"\r\n#Customize the \u201cfrom\u201d address\r\nMAIL_FROM=\"From: YOURNAME@gmail.com\"\r\n#Verify that ipaddress file exists, compare to the current IP, update ipaddress file\r\nif [ -f $IPFILE ]; then\r\nKNOWN_IP=$(cat $IPFILE)\r\nelse\r\nKNOWN_IP=\r\nfi\r\nif [ \"$CURRENT_IP\" != \"$KNOWN_IP\" ]; then\r\necho $CURRENT_IP &gt; $IPFILE\r\n#Customize email addresses and Gmail password\r\nsendEmail -f \"$MAIL_FROM\" -t RECIPIENT@YOURDOMAIN.COM -u \"$CURRENT_IP\" -m \"$CURRENT_IP\" \"Local time is\" \"$DATUM\" -s smtp.gmail.com:587 -xu YOURNAME@gmail.com -xp GMAIL-PASSWORD -o tls=yes &gt; \/dev\/null\r\nlogger -t ipcheck -- IP changed to $CURRENT_IP\r\nelse\r\nlogger -t ipcheck -- No IP change\r\nfi<\/pre>\n<p><strong>Step-by-step Instructions<\/strong><\/p>\n<p>Create a directory called \u201cbin\u201d in your home directory unless one already exists. Create a new text file, copy and paste the above script into the file. Customize your Gmail information and save the script as \u201cdynip.\u201d Right click the script and give it a permission to execute. If that doesn&#8217;t work in your flavor of Linux, open up the terminal window and type these commands:<\/p>\n<p><em>cd \/home\/yourusername\/bin<\/em><br \/>\n<em> chmod +x dynip<\/em><\/p>\n<p>Now we need to make it possible for the operating system to execute your script from the command line regardless of your current directory. We&#8217;ll add the directory you created to the PATH system variable:<\/p>\n<p><em>export PATH=$PATH:\/home\/yourusername\/bin<\/em><\/p>\n<p>Next, we&#8217;ll install the sendEmail package unless you already have it. In Debian\/Ubuntu you can use:<\/p>\n<p><em>sudo apt-get install sendEmail<\/em><\/p>\n<p>If everything went right\u00a0we can test the script by typing \u201cdynip\u201d at the command prompt. The script will wait for 60 seconds and email the IP address to the address specified within.<\/p>\n<p><strong>On Boot Scheduling<\/strong><\/p>\n<p>The script is now working at the command line level, but we need to add it to your crontab so it runs automatically each time you log into the computer. Crontab is a text file which keeps track of scheduled jobs. To edit the crontab:<\/p>\n<p><em>crontab -e<\/em><\/p>\n<p>Add a line of text:<\/p>\n<p><em>@reboot \/home\/yourusername\/bin\/dynip<\/em><\/p>\n<p>and save crontab. This will cause your computer to ryn dynip script every time you log in. It is possible to <a href=\"https:\/\/help.ubuntu.com\/community\/CronHowto\" target=\"_blank\">setup the same or similar automatic jobs<\/a> for other users as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Automatically Email WAN IP Address at Login &#8211; Linux Note that after this post was written whatsmyip.com has stopped providing support for IP detection automation. You may consider using\u00a0icanhazip.com instead. While I can only take a partial credit for this bash script which emails the WAN IP address at login, I expanded it and modified &hellip; <a href=\"https:\/\/www.skifactz.com\/wifi\/?p=54\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Email IP Address on Login<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-software"],"_links":{"self":[{"href":"https:\/\/www.skifactz.com\/wifi\/index.php?rest_route=\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skifactz.com\/wifi\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skifactz.com\/wifi\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skifactz.com\/wifi\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skifactz.com\/wifi\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=54"}],"version-history":[{"count":2,"href":"https:\/\/www.skifactz.com\/wifi\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":56,"href":"https:\/\/www.skifactz.com\/wifi\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions\/56"}],"wp:attachment":[{"href":"https:\/\/www.skifactz.com\/wifi\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skifactz.com\/wifi\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skifactz.com\/wifi\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}