Email IP Address on Login

Automatically Email WAN IP Address at Login – Linux

Note that after this post was written has stopped providing support for IP detection automation. You may consider using 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 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.

# Emails current IP address to a recipient. Requires package sendEmail with SSL and TLS.
#Last known IP address
#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 -
#Customize the “from” address
#Verify that ipaddress file exists, compare to the current IP, update ipaddress file
if [ -f $IPFILE ]; then
if [ "$CURRENT_IP" != "$KNOWN_IP" ]; then
#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 -xu -xp GMAIL-PASSWORD -o tls=yes > /dev/null
logger -t ipcheck -- IP changed to $CURRENT_IP
logger -t ipcheck -- No IP change

Step-by-step Instructions

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:

cd /home/yourusername/bin
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:

export PATH=$PATH:/home/yourusername/bin

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:

crontab -e

Add a line of text:

@reboot /home/yourusername/bin/dynip

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.

Leave a Reply

Your email address will not be published. Required fields are marked *