Mail My IP

I intend to put my Raspberry Pies to work as intelligent controllers. As long as they stay at home I can easily keep track of the IP addresses they get from my modem. However as soon as my Pies start roaming the world and get connected to strange networks it is not as simple any more.
This little script will help you determine the IP address the Raspberry Pi has received. As soon as the Raspberry Pi receives an IP address from the network's DHCP server it will send a mail, telling you what IP address it has received. It will even tell you with what IP address it is now connected to the internet, including its IPv6 address, if has got one.

Before you can use this script you should set up a mail account on your Raspberry Pi. You can read elswhere on my site how to set up a Gmail account on your Raspberry Pi.

The Mailmyip Script

Below you see the listing of the mailmyip script. Copy and paste it in your Pi's text editor and change 2 configuration variables. RPINAME holds a name with which you can identify your Raspberry Pi's mails, just in case you've got more than one mailing Raspberry Pi. MAILTO holds the email address you want the messages to be sent to.

#! /bin/bash

# mailmyip
#
# This script sends a mail every time the network comes up.
# The mail contains a time stamp and the obtained IP address.
#
# Use: mailmyip [interfacename]
#
# Author: San Bergmans
#         www.sbprojects.net
#

# Configuration variables
RPINAME="Raspberry Pi"
MAILTO="[email protected]"

# Get interface name from parameters
if [ $# -eq 0 ]
then
        IFC="eth0"
else
        IFC="$1"
fi

ifconfig $IFC &> /dev/null
if [ $? -ne 0 ]
then
        exit 1
fi

# Get current private IP address for the selected interface
PRIVATE=$(ifconfig $IFC | grep "inet addr:" | awk '{ print $2 }')
IPV6=$(ifconfig $IFC | grep "Scope:Global" | awk '{ print $3 }')
PRIVATE=${PRIVATE:5}

# Exit if IP address is empty
if [ -z $PRIVATE ]
then
    exit 0
fi

# Wait about 2 minutes for the RTC to be set after boot (in steps of 10 seconds)
for I in {1..12}
do
    sleep 10
    if [ $(date +%Y) != "1970" ]
    then
        # Yes! The clock is set. Find out what our public IP address is and send the message.
                PUBLIC=$(curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
        MSG="$(date +%F\ %T)\n$RPINAME $IFC now has IP-Address $PRIVATE.\nIts public address is $PUBLIC.\n$IPV6"
        echo -e $MSG | mail -s "$RPINAME just received a new IP address" "$MAILTO"
        exit 0
    fi
done

First the script will try to find out what network card's IP address you're interested in. If you didn't provide a network card when calling the script, it will default to eth0. The script will simply end if you provide a non existent network card name.
Then the script filters the private IP address from the output of the ifconfig command. In case no IP address has been received, the script will simply exit. No IP address means no connection to the internet. No connection to the internet means we won't be able to mail anyway.
Then the script enters a for loop, which is repeated a maximum of 12 times. Each loop lasts 10 seconds, which means that the loop can take up to 2 minutes to execute. During this delay we wait for the Real Time Clock to get a valid time. Just after booting the Raspberry Pi will have an invalid time (date is 1970) because it has no hardware clock. The script will mail the current IP address as soon as the date is no longer 1970. After that the script will exit.
If the clock isn't set within 2 minutes the script will end, without mailing anything.

The if-up Hook Script

So far we've got a script, but that won't run of its own accord. Previously I have used the so called if-up hook directory. This is a special directory called /etc/network/if-up.d. Any script put in there will be run whenever a network card comes up, any network card. This limits your control on what network cards you're interested in. I'm not interested in the lo network card for instance.

I now call the script by the post-up in /etc/network/interfaces. Below you'll find an example interfaces file. Simply add the post-up after every interface you're interested in.

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
post-up /home/pi/bin/mailmyip.sh &

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
post-up /home/pi/bin/mailmyip.sh wlan0 &

Important note: The auto ... lines are necessary for the post-up scripts to be executed!

Please note that this is only an example file, showing you where and how to call the mailmyip script.