Automatically Email WAN IP Address at Login – Linux
Note that after this post was written whatsmyip.com has stopped providing support for IP detection automation. You may consider using icanhazip.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 it for use with Gmail. The basic idea is that your Linux machine retrieves the internet IP address from www.whatsmyip.org and sends it out to an email recipient.
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.
Other uses may include detection of unauthorized use, GPS-free geo tracking, etc.
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.
#!/bin/bash # Emails current IP address to a recipient. Requires package sendEmail with SSL and TLS. #Last known IP address IPFILE=/tmp/ipaddress #Allow 60 seconds for the Internet connection to become active sleep 60 #Retrieve and assign WAN IP address to CURRENT_IP CURRENT_IP=$(wget -q -O - http://automation.whatismyip.com/n09230945.asp) DATUM="$(date)" #Customize the “from” address MAIL_FROM="From: YOURNAME@gmail.com" #Verify that ipaddress file exists, compare to the current IP, update ipaddress file if [ -f $IPFILE ]; then KNOWN_IP=$(cat $IPFILE) else KNOWN_IP= fi if [ "$CURRENT_IP" != "$KNOWN_IP" ]; then echo $CURRENT_IP > $IPFILE #Customize email addresses and Gmail password sendEmail -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 > /dev/null logger -t ipcheck -- IP changed to $CURRENT_IP else logger -t ipcheck -- No IP change fi
Create a directory called “bin” 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 “dynip.” Right click the script and give it a permission to execute. If that doesn’t work in your flavor of Linux, open up the terminal window and type these commands:
chmod +x dynip
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’ll add the directory you created to the PATH system variable:
Next, we’ll install the sendEmail package unless you already have it. In Debian/Ubuntu you can use:
sudo apt-get install sendEmail
If everything went right we can test the script by typing “dynip” at the command prompt. The script will wait for 60 seconds and email the IP address to the address specified within.
On Boot Scheduling
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:
Add a line of text:
and save crontab. This will cause your computer to ryn dynip script every time you log in. It is possible to setup the same or similar automatic jobs for other users as well.