RPi Email IP On Boot Debian

Back to RPi Guides.

Send Email Containing IP Address On Boot

=What does it do?=

This code will extract the ip address of your Pi and then send an email containing the ip to the specified email address. This is inspired by the need to access the Pi via SSH or other network protocols without a monitor and moving from network to network. This assumes a Gmail SMTP server. You may need to alter a bit for other servers (beyond scope here)

=What do you need?=

A working, and network enabled Raspberry Pi

=What skill level is required?=

Medium Level. You should be comfortable navigating a linux system and be comfortable using sudo (if you want to use this script, odds are you are quite comfortable at the command prompt).

=Overview of this guide= You need to
 * Create a python script and store it in a Directory
 * Make python script executable
 * Edit /boot/boot.rc

=Let's Do It=

Create the python script
Copy and paste the following code into a text editor (I'm a nano man myself)

import subprocess import smtplib import socket from email.mime.text import MIMEText import datetime to = 'me@example.com' gmail_user = 'test@gmail.com' gmail_password = 'yourpassword' smtpserver = smtplib.SMTP('smtp.gmail.com', 587) smtpserver.ehlo smtpserver.starttls smtpserver.ehlo smtpserver.login(gmail_user, gmail_password) today = datetime.date.today arg='ip route list' p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE) data = p.communicate split_data = data[0].split ipaddr = split_data[split_data.index('src')+1] my_ip = 'Your ip is %s' % ipaddr msg = MIMEText(my_ip) msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y') msg['From'] = gmail_user msg['To'] = to smtpserver.sendmail(gmail_user, [to], msg.as_string) smtpserver.quit
 * 1) Change to your own account information
 * 1) Very Linux Specific

Save this script using a nice name like 'startup_mailer.py' and make note of its path (like /home/pi/Code/startup_mailer.py)

For good measure, make the script executable sudo chmod +x startup_mailer.py

Edit /boot/boot.rc
Using your text editor once again, edit /boot/boot.rc (this assumes you have already renamed this file to boot.rc If not, see RPi_Advanced_Setup). For example: sudo nano /boot/boot.rc

Add the following at the end of the file, making changes to the path for your directory tree and save. python /home/pi/Code/startup_mailer.py
 * 1) Script to email ip address upon reboot

Alternative if using Rasbian
If you are using Rasbian you won't have a /boot/boot.rc file. Instead you can edit /etc/rc.local as follows:

sudo nano /etc/rc.local

Add the python line so the file now looks like this:

# rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # Print the IP address if it doesn't work ad sleep 30 before all your code _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" python /home/pi/Code/startup_mailer.py fi  exit 0

Finish up
Reboot your Pi and you should receive an email with your ip address

Trouble shooting
+ you may add (sleep 30) in the etc/rc.local just after the # if you don't get your email notification hang your pi to a monitor and check at bootup if it shows My ip adresss is .... if it doesn't do that! sleep 30. etc/rc.local is linked to etc/init.d/rc.local it's an special service that starts on bootup but it seems there are some bugs in it.

+ If you don't get email when rebooting, you have to check the hostname you currently have. because the script calls for #raspberrypi. Just type in the command line # hostname. if you have an other hostname its simple just change you hostname #with your preferred editor. Also in etc/hosts on the bottom of the page

$ msg['Subject'] = 'IP For YOUR HOSTNAME on %s' % today.strftime('%b %d %Y')