Get an Email with your IP Address

I often struggle with finding the IP address of my Raspberry Pi.  I usually need to plug it into a monitor, get its address, and then log in to it through SSH.  It is a hassle, and it is significant enough to delay a lot of my work.

Not anymore!

I combined a couple programs that will:

  1. Use a python script to get the IP address of the Raspberry Pi.
  2. Use a python script to send me an email with the IP address in the body.
  3. Do this every time the Raspberry Pi boots up.

There are some pre-requisites for being able to do this.  First you will need to be sure that your Raspberry Pi can send email from the command line.  I have written a tutorial on this, and you will need to do this before attempting this tutorial.

Finding the IP address

If your Raspberry Pi is online, you can find the IP address by typing

ifconfig

This will return a lot of information, including the IP address if you are online through a wired ethernet connection (eth0) or through a wireless connection (wlan0).  As you can see, I have an IP address on the wireless network.  Knowing that IP Address is handy, because it gives me the opportunity to control the Pi from another computer through SSH.

Again, my goal is to find the IP address using a python program.  When I get that as an output, I can embed it in an email.  So I did some searching, and found this program for getting the IP address.

Lets create a file called ipaddress.py.

nano ipaddress.py

Copy and paste the program below into that file.  We will go over what it does below.

import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  
        struct.pack('256s', ifname[:15])
    )[20:24])

address = get_ip_address('wlan0') 
#change wlan0 to eth0 if you want the wired IP address 
print(address) 

This program uses a pre-packaged library (socket) to get the IP address.  When the method is run it returns the IP address of the eth0 or wlan0.  That output is captured in the variable ‘address’ and then printed out.  You should run this to make sure you are getting the IP address you expect.

python ipaddress.py

Adding this Address to an Email

Again, you need to be sure your Raspberry Pi can send email using SMTP.  This tutorial will walk you through it.

We are going to add some code to the program above.  Again, this came from a quick Google search with other people having similar problems.  I have used other approaches, but this one worked out the best.

This code will be added to our code from above.

import smtplib
fromaddr = 'user_me@gmail.com' #gmail address this is coming from
toaddrs  = 'user_you@gmail.com' #the address this is going to
msg = "\r\n".join([
  "From:user_me@gmail.com", #gmail address this is coming from
  "To: user_you@gmail.com", #address this is going to
  "Subject: The RPi IP Address", #this could be anything
  "",
  "Message text" #body of the message
])
username = 'user_me@gmail.com' #your gmail login (usually email address)
password = 'pwd' #the app specific password, not your user password
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

In this code, python is being used to send an email.  If you save this as its own program, it would be a good test to make sure it works.  Assuming it does, you can combine the two programs.  Open ipaddress.py again.

nano ipaddress.py

And copy the mail program in.  At the end it should look like this

import socket
import fcntl
import struct
import smtplib

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

address = get_ip_address('wlan0') 
#change wlan0 to eth0 if you want the wired IP address 
#print(address) 

fromaddr = 'user_me@gmail.com' #gmail address this is coming from
toaddrs  = 'user_you@gmail.com' #the address this is going to
msg = "\r\n".join([
  "From:user_me@gmail.com", #gmail address this is coming from
  "To: user_you@gmail.com", #address this is going to
  "Subject: The RPi IP Address", #this could be anything
  "",
  "The IP Address of the Raspberry Pi is:", #body of the message
  address
])
username = 'user_me@gmail.com' #your gmail login 
password = 'pwd' #the app specific password, not your user password
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

Here the programs are combined, and I made a couple changes.  I updated the message text to say ‘The IP Address of the Raspberry Pi is:’ and then the address variable gets dumped in on the next line.  If this works, you would get a two line email.

Start at Reboot

What really makes this program handy is that it can run whenever the Pi gets turned on.  That way, the Pi would get plugged in, go online, get an IP address and tell me what it is via email.

To do that we will need to set up a cron job, which is something that happens at regular intervals that you can set.

sudo crontab -e

That will open up a document.  At the bottom you can put this command.

@reboot sleep 60 && python /home/pi/ipaddress.py

Close the document.  Reboot.  A little more than one minute after it reboots, you will get an email from the Pi that will give you the IP Address.

NOTE:  One important thing to note is that this will really only work on a network that doesn’t require any special permission to join.  So a Raspberry Pi would need to be able to get on to a network in order to get an IP address and send an email.  I wrote another tutorial about adding wireless networks to your Raspberry Pi through the command line.  That works for things like home networks, or other password protected networks.  You will need to configure the Pi to work on those.