Tweet 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 that 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 Tweet, telling what IP address it has received. It can even tell you with what IP address it is now connected to the internet if you want to.
Tweet my IP Example

The Tweetmyip Script

Below you see the listing of the script. Copy and paste it in your Pi's text editor and change 2 or 3 of the configuration variables. RPINAME holds a name with which you can identify your Raspberry Pi's tweets, just in case you've got more than one tweeting Raspberry Pi. TWEET holds the name of your twitter sending script, including its path, which can be found elsewhere on my site. If you want to use a specific identity to tweet from you can include the -i identity argument at the end of this variable. PUBLICIP holds either "yes" or something else. If it holds "yes" your Pi will tweet both its private and public IP addresses. If it holds something else, like "no", it will only tweet its private IP address.

#! /bin/bash

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

# Configuration variables
TWEET="/home/pi/bin/ -i identity -m"

# Get interface name from parameters
if [ $# -eq 0 ]

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

# Get current private IP address for select interface
PRIVATE=$(ifconfig $IFC | grep "inet addr:" | awk '{ print $2 }')
IPV6=$(ifconfig $IFC | grep "Scope:Global") | awk '{ print $3 }' | awk -F/ '{ print $1 }'

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

# Wait about 2 minutes for the RTC to be set after boot (in steps of 10 seconds)
for I in {1..12}
    sleep 10
    if [ $(date +%Y) != "1970" ]
        # Yes! The clock is set. Now it's time to send the tweet
        if [ $SHOWPUBLICIP = "yes" ]
            PUBLIC=$(curl -s|sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
            MSG="$(date +%F\ %T) $RPINAME $IFC now has IP-Address $PRIVATE. Its public address is $PUBLIC."
            MSG="$(date +%F\ %T) $RPINAME $IFC now has IP-Address $PRIVATE"
        $TWEET "$MSG"
        exit 0

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 tweet 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 tweet 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 tweeting anything.

The script also collects the IPv6 address of your Raspberry Pi, however it won't tweet that. I don't like my private address to be tweeted, for obvious security reasons. A global IPv6 address can be just as dangerous to reveal to the world as your public IPv4 address.
If you don't mind that your public address is known to the entire universe, feel free to add the IPv6 address to the output too.

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 line after every interface you're interested in.

auto lo
iface lo inet loopback

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

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/ 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 script.