Clay Chaplin
  • Home
  • Work
  • About
  • Events
  • News

Raspberry Pi – Email IP Address

  • March 1, 2014
  • Raspberry Pi

When running a Pi headlessly it’s helpful to find out the IP address without having to connect the Pi to a display. A Python script can be executed on boot to send you an email that contains the Pi’s IP address. You’ll need to edit the Python script that is linked below and one other file on your Pi to make this happen. You also need to know the Pi’s current IP address.

Download the following python script to your laptop:

startup_mailer

Once downloaded, change the name to “startup_mailer.py”
Open the file in a text editor on your laptop and add information to the following variables:
     to – this is your email address
     gmail user – this is your gmail username
     gmail password – this is your gmail password
Save the file to your desktop

It’s probably pretty obvious but you have to use a Gmail account. In the next step you will upload the python script from your laptop to your Pi.

Upload the script to your Pi using a new ‘local’ terminal window on your laptop
In the Terminal program on your laptop type command-t to open another session window. Make sure you are typing commands to your laptop and not the Pi! Check the command line prompt at this point.
In the new / local terminal window type:

 scp /users/username/desktop/startup_mailer.py pi@ip-adress:/home/pi/scripts

make sure to edit the username and ip-address to match your settings. The command, assuming startup_mailer.py is on the desktop, looked like this on my laptop:

scp /users/dwingus/desktop/startup_mailer.py pi@10.0.1.20:/home/pi/

Back on the Pi’s terminal window (check the prompt) move to your home directory

cd /home/pi

Make the python script executable

sudo chmod +x startup_mailer.py

Make a new directory called scripts

sudo mkdir scripts

Move the startup_mailer.py file to the scripts directory

sudo mv startup_mailer.py scripts

Now the python script is in place and is ready to be executed.

Edit the rc.local file that will call the startup_mailer.py script when the Pi boots

Open the file rc.local in the nano text editor

sudo nano /etc/rc.local  

Add the following right above “fi”:

python /home/pi/scripts/startup_mailer.py

Save the file

rc.local is a file that is called at the end of the Pi’s boot sequence. You’ve placed a command in rc.local that calls Python to execute the startup_mailer script that ultimately emails you the Pis IP address.

TroubleShooting
If you find that you do not receive an email when the Pi boots try a couple of things:
– confirm that you entered all of your email information correctly in the python script

 sudo nano /home/pi/scripts/startup_mailer.py


- make sure your Pi is connected to the internet

 ip addr


- try running the script directly from Python and then check your email

sudo python /home/pi/scripts/startup_mailer.py

- If you get an email when running the script directly as in the line above try adding a pause to the rc.local file.
Open the file rc.local in the nano text editor

sudo nano /etc/rc.local  

Add the following right after # Print the IP Address

sleep 5 

This will cause the system to wait 5 seconds before executing the python script. Sometimes the Pis need just a hair more time to establish a network connection.

Tags:

© 1998 - 2025 Clay Chaplin