Raspberry Pi Notes

Send an email when it boots

 

 

 

My Arduino AlaMode Notes

My Arduino Bare Bones Board

General Raspberry Pi Notes

Other Rants and Raves

 

March 2013 - When my RPi boots it grabs an IP address from the local network. Running headless, I don't know what address it has. I could Snap on my iOS device to scan the subnet for the RPi, but that isn't very elegant. Instead I've set the RPi up to email me its IP address when it boots up. I look at the email and can then shell into the device.

I needed a couple of things to do this:

  1. Find the current IP address
  2. Send the current IP address to my email address
  3. Do all this at boot

Find the current IP Address

ifconfig gives information on your network interfaces. Any interface that has an IP address will have the text "inet addr" in one line. We use grep to get those lines:<.p>

/sbin/ifconfig | grep '.*inet addr.*'

Send email

It's not hard to send email. Of course, you have to have an SMTP server somewhere. I have one on my domain, so I used that. You'll have to put your own in this code. And note that your SMTP server might use a non-standard port; just put the port in the right place.

TOADDR="TONAME@ADDRESS.COM"
EMAILSERVER="smtp.YOURSERVER.COM:THEPORT"
FROMADDR="FROMNAME@ADDRESS.COM"
MESSAGE="the message will go in this variable."

sendemail -f $FROMADDR  -t $TOADDR -u "Test Email Subject Line" -m $MESSAGE -s $EMAILSERVER 

Create a Boot Script

It takes a while for the RPi to acquire an IP address - at least that's what I've experienced with my WiFi. To allow for this I put the ifconfig command in a loop. The loop exits when the IP address is reported, or after a number of iterations.

The Debian distro I'm running needs some information at the start of each boot script, so I put that here too.

Copy this script into a file named bootEmailIPAddress in your docs folder. Remember to replace the variables values at the start of the script with your own data. Note that the increment of TRYLOOP uses those odd ball back ticks NOT single quotes.

### BEGIN INIT INFO
# Provides:	boot ip email
# Required-Start:	$syslog
# Required-Stop:	$syslog
# Default-Start:	2 3 4 5
# Default-Stop:	        0	
# Short-Description:    Upon boot will email the ip address
# Description:		Modify this script to set TOADDR to be your own email
#                       address. Each time the RPi boots you'll get an email.
#                       Update EMAILSERVER to be your server.
### END INIT INFO
# Author: Jim Schrempp Jim AT Jimschrempp.com 2013
#
# If you modify this, please add your contact info
#
#
#  Send email of ip address upon boot up
#

#! /bin/bash

TOADDR="TONAME@ADDRESS.COM"
EMAILSERVER="smtp.YOURSERVER.COM:THEPORT"
FROMADDR="FROMNAME@ADDRESS.COM"

TRYLOOP=1
while [ $TRYLOOP -gt "0" ]
  do

    TRYLOOP=`expr $TRYLOOP + 1`
    if [ $TRYLOOP -gt "30" ]
    then 
      TRYLOOP=0
    fi

    MYIP=$(/sbin/ifconfig | grep '.*inet addr.*')

    if [ -z "$MYIP" ]
      then
         sleep 10s
      else
         TRYLOOP=0
    fi

done

echo 'ip is:'
echo $MYIP

if [ -n "$MYIP" ]
  then
    sendemail -f $FROMADDR  -t $TOADDR -u "RPi IP $MYIP" -m $MYIP -s $EMAILSERVER 
fi

Get The Script Into The Boot Sequence

First copy the script into /etc/init.d

sudo cp bootEmailIPAddress /etc/init.d

Then get it wired into the boot sequence

sudo update-rc.d bootEmailIPAddress defaults

Check and Go

Now boot your RPi and watch the messages that come up. Near the end, just before it prompts you to login, you should see messages that bootEmailIPAddress is running. Wait a few seconds and you should have an email from your RPi!

When I did this the first few times I had an error in my boot script and was afraid I'd never get my RPi back without a complete remake of the SD card. Luckily I had all my own stuff checked into my Subversion repository, so it would be painful but not a disaster. (You DO use subversion, right?) From Googlin' around I found that a jumper between GPIO pins 5 & 6 will cause the RPi to boot into safe mode and allow you to fix that bad boot script.


Jim Schrempp is a sometimes freelance writer (only Vanity Press will publish his work) living in Saratoga, California. His writings have appeared on numerous pages on his own web site. The opinions expressed in this piece are those of the writer and do not necessarily represent those of anyone else (although Jim wishes more people shared his opinions)