I do a lot of Raspberry Pi projects – often creating them in one place and using them in another. While it is easy enough to use SSH, VNC or HTML to access a device, if it’s being moved from one network to another, it can be challenging to know its ip address. Sure, you may be able to log into a local router and look it up, but I thought it would be easier if I just had the RPi email me with its current ip address.
Thankfully, Cody Giles created a tutorial on elinux.com outlining how to do this. His Python script runs on the RPi on startup, finds its ip address(es) and then emails this information to a predetermined address. I started with his code, but ended up changing it to meet my needs.
In order to allow a Python script to send email through a Gmail account, you may need to enable an “App Password” for your sending account.
Step #1: Create the Python Script
Copy the following and paste it into a text editor. You must change the ‘to’ address, as well as the ‘gmail_user’ and ‘gmail_password’ for the sending account in the lines below the pound sign lines about 25 lines from the top. Save this file as a python script in the home directory named startup_email.py.
import subprocess
import smtplib
from email.mime.text import MIMEText
import datetime
import os
# Account Information
##################################################################
to = 'recipientemail@server.com' # Email to send to.
gmail_user = 'senderaddress@gmail.com' # Email to send from. (MUST BE GMAIL)
gmail_password = 'emnopqoodnzcvkir' # Gmail app password.
smtpserver = smtplib.SMTP('smtp.gmail.com', 587) # Server to use.
smtpserver.ehlo() # Says 'hello' to the server
smtpserver.starttls() # Start TLS encryption
smtpserver.ehlo()
smtpserver.login(gmail_user, gmail_password) # Log in to server
today = datetime.date.today() # Get current time/date
arg='ip route list' # Linux command to retrieve ip addresses.
# Runs 'arg' in a 'hidden terminal'.
p=os.popen("ifconfig | grep 'inet '").read()
#data = p.communicate() # Get data from 'p terminal'.
print("p=")
print(p)
print("END p")
iplist = os.system("ifconfig | grep 'inet '");
print("iplist = "+str(iplist))
print("******************")
print(iplist)
# Creates the text, subject, 'from', and 'to' of the message.
msg = MIMEText("IP Addresses:" + "\n" + str(p))
msg['Subject'] = 'IPs For RaspberryPi on %s' % today.strftime('%b %d %Y')
msg['From'] = gmail_user
msg['To'] = to
# Sends the message
smtpserver.sendmail(gmail_user, [to], msg.as_string())
# Closes the smtp server.
smtpserver.quit()
Step #2: Make the script executable.
Using Terminal, navigate to the home directory and make the script executable.
cd ~
sudo chmod +x startup_email.py
Setp #3: Run the Script on Startup.
We’ll add a line to boot.rc to run the script whenever the RPi boots.
sudo nano /boot/boot.rc
Add the following lines to boot.rc
python /home/pi/Code/startup_email.py
Now, reboot the Pi and in about a minute, you should receive an email with your device’s IP address.