Tutorial 10: Email Yourself your IP Address

You have your Raspberry Pi web server online and now it is secured. Now how do you find it?

Most websites you visit have a static IP address - that is: an IP address that does not change. So when you type in "google.com" you are actually visiting "64.233.169.138", the address of google.com. Static IP addresses are not offered by default by ISPs; instead they assign you a changing or dynamic IP address so you can navigate online. This dynamic IP address of yours may last a week, a day, or even just an hour. This makes it difficult to visit the server you're trying to host on your Pi!

In this tutorial I show you how to write a Python script that automatically checks your IP address and then sends the IP address to a gmail email account. Why? Well, regardless of where you are you will be able to access your email. If you have the current dynamic home IP address you will be able to access your server! A simple work around that won't cost you anything.

This tutorial is a little older (published in 2015!). The main principles are still relevant, so I haven't needed to update this lesson.


DIFFICULTY
MODERATE
LINUX UNDERSTANDING
MODERATE
COMPUTER FAMILIARITY
SOME
PYTHON PROGRAMMING
SOME
ABOUT
0
MINUTES
  • How to automatically check your external IP address
  • How to send an email using Python
  • How to automate a task on the pI (schedule a task) using chron
  • How to retrieve your external IP address through email
This tutorial features Python 3. The code can be found below:


from urllib.request import urlopen
import re
import smtplib
import sm

# Setup our login credentials
from_address = ''
to_address = ''
subject = 'Pi IP'
username = ''
password = ''

# Setup where we will get our IP address
url = 'http://checkip.dyndns.org'
print ("Our chosen IP address service is ", url)

# Open up the url, then read the contents, and take away our IP address
request = urlopen(url).read().decode('utf-8')
# We extract the IP address only
ourIP = re.findall("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", request)
ourIP = str(ourIP)
print ("Our IP address is: ", ourIP)

def send_email(ourIP):
# Body of the email
    body_text = ourIP + ' is our PlayPi IP address'
    msg = '\r\n'.join(['To: %s' % to_address, 'From: %s' % from_address, 'Subject: %s' % subject, '', body_text])

    # Actually send the email!
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls() # Our security for transmission of credentials
    server.login(username,password)
    server.sendmail(from_address, to_address, msg)
    server.quit()
    print ("Our email has been sent!")

# Open up previous IP address (last_ip.txt) and extract contents
with open('/home/pi/ipemail/last_ip.txt', 'rt') as last_ip:
    last_ip = last_ip.read() # Read the text file

# Check to see if our IP address has really changed
if last_ip == ourIP:
    print("Our IP address has not changed.")
else:
    print ("We have a new IP address.")
    with open('/home/pi/ipemail/last_ip.txt', 'wt') as last_ip:
        last_ip.write(ourIP)
    print ("We have written the new IP address to the text file.")
    send_email(ourIP)

You shouldn’t need any supporting content to complete this tutorial.