Pi Fillings Part 1: IP Phone Home

Headless Raspberry Pis (those with no monitor attached) are wonderful tools, especially for remote work. But what if you don't have a static network arrangement and the Pi's IP is different on every boot? Hooking a monitor in at every boot is annoying, and a display to show the IP is expensive and time-consuming. I use a simple method to have my Pi text me its IP address every time it boots that can be easily extended to emailing or sending other information.


The RasPi's IP address will be printed by a C program and piped to the mail program. The address will be sent via email to either a specified personal email address or, using carrier SMS gateways, to a phone number as a text message. The program will be called on the completion of bringing network interfaces up.


  • I'm assuming that you have already set up and installed Rasbpian on a RasPi and you also know how to use SSH. If not, Google around until you do.
  • I'm also assuming you're familiar with the basics of command-line work and know how to edit files using nano or another command-line text editor. I use nano because I like nano.
  • For this tutorial, after running the standard sudo apt-get update && sudo apt-get upgrade you'll want to run sudo apt-get install gcc mailutils ssmtp. I'll explain what those packages are later.
  • You will need an email account; I recommend a Gmail account. It can be a personal account, throwaway account, whatever.


  1. [OPTIONAL] Set up whatever email account you're going to use, be it a Gmail account or otherwise.  This tutorial will handle Gmail.
  2. First we'll configure SSMTP to get it working properly.
    1. Edit /etc/ssmtp/ssmtp.conf to edit the SSMTP config file once it's installed (you'll need root privileges).  It should look something like this: ``` root=@gmail.com mailhub=smtp.gmail.com:587 rewriteDomain= hostname=@gmail.com UseSTARTTLS=YES AuthUser=@gmail.com AuthPass= FromLineOverride=YES ```
    2. Now we'll edit (again, root privileges needed) /etc/ssmtp/revaliases.  Add a line to the end of the file for each user with which you want to be able to send mail; I did pi and root to be safe.  The lines should look something like this: ``` root:@gmail.com:smtp.gmail.com:587 pi:@gmail.com:smtp.gmail.com:587 ```
    3. You can test it out now by piping something to the mail command, as in: echo "This is a test" | myemail@foo.com  You should receive an email shortly as long as your Pi is connected to the Internet.
    4. If you got an error about the command mail not existing, make sure you installed mailutils.  I assumed mail functionality was installed by default on almost every Linux box but apparently it's not in recent Raspbian builds.
  3. Next up we're going to be setting up the program and script that pipes the internal IP address to mail.  I initially did by parsing the output from ifconfig with grep or awk and stripping out the line containing the IP.  This didn't work as well when the RasPi was connected through Ethernet instead of WiFi, though, so I did a little research and found out that you can easily get network info through a few C system calls.  Download and look through the code here (I adapted it from a StackOverflow answer while researching the proper functions); I tried to explain it reasonably well through comments, but remember, the man pages are always your friend.  Compile it by running gcc print_ips.c -o print_ips.  If you run it (./print_ips) you should see your Pi's IP address and the interface type printed to the screen.
  4. Now let's create a quick little script to automate the process.
    1. First we're going to figure out what the destination of the text messages will be.  This page is pretty much all you need to look at.  For instance, I'm a Verizon customer, so the address I use is xxxyyyyyyy@vtext.com where xxx is my area code and yyyyyyy is my cell number.
    2. Create the script in the same directory as print_ips: nano textip.sh
    3. The script will be pretty simple, with just a single command to pipe the output of our program to mail: ./print_ips | mail xxxyyyyyyy@carrier.com
    4. Run chmod +x textip.sh.  This makes the script executable.
  5. Time for the final step in the process: having the script automatically fire when the network is connected on boot.  I toyed around with runlevel scripts a bit for this but was concerned that it wouldn't fire at the right time (for instance, before the network interfaces had been brought up).  Turns out there's a post-up functionality that runs commands exactly after the network connection's been established.
    1. Open up (root privileges again!) your interfaces file: /etc/network/interfaces.
    2. Each network interface definition is started by an iface line.  At the end of every definition, add the line post-up /path/to/textip.sh.
    3. For reference, here's my interfaces file.  I have the script run when either the ethernet or wireless interfaces is brought up, so the line appears twice. ``` auto lo

      iface lo inet loopback
      iface eth0 inet dhcp
      post-up /home/pi/scripts/textip.sh

      allow-hotplug wlan0
      iface wlan0 inet dhcp
      wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
      post-up /home/pi/scripts/textip.sh
      iface default inet dhcp

      <li>That should be it!&nbsp; Restart your Pi to see the script in action.&nbsp; If you have questions or issues or I've messed something up--because I undoubtedly have--post a comment!&nbsp; I'll do my best to help out.</li>