Step by step guide on how to send the IP address of your Raspberry Pi to your Gmail account when the board starts. This way it is easier to find and access over a local area network from other devices.
This post is dedicated to those who access their Raspberry Pi from another device, like a laptop for example. It does not matter if you use SSH (Secure Shell), VNC (Virtual Network Computing ) or any other way to reach your Raspberry Pi.
I bet you found yourself at least once in the following situation. You plug in the Ethernet cable, power up the Pi and start wondering what IP address got assigned this time to your little board.
At the moment of writing this tutorial I own a Raspberry Pi 3 Model B running Raspbian Jessie with PIXEL. All the following information may also apply to future Raspbian releases .
You could just fire up a browser, open up the web interface of your home router and check there. The DHCP client list does not lie, right? Or even better, you can assign a static IP address to your Raspberry Pi. This can be done either from the router settings by always leasing the same IP address or from the Pi itself. But what if you don’t have this privilege? What if you take the Raspberry Pi to a school or university lab? You may not have the rights to access the list of devices connected to the network.
There are also brute force methods to find the IP address of the Raspberry Pi. Pinging the whole network is one possible solution. There are even tools that do this for you. I used Angry IP Scanner for an extended period and it really works. You fire up the tool, set the address range, press a button and wait. After you finish the scan you just need to look for your Raspberry Pi. However there is a slight chance that this will not work. The network administrator can filter out ICMP packets and you can’t ping other machines. Also, by aggressively pinging the network you may upset someone. I would resort to this method only if there is no other way around.
My solution to this problem revolves around sending out a mail at every boot containing the IP of the device. This tutorial uses Gmail but you can use any other solution that supports sending mails over SMTP. The only downside of this solution is that the Raspberry Pi needs to be connected to the internet. I have split the tutorial in two separate sections. The first one covers the mail setup while the second one covers the automation that sends the IP address. Sending mails from the Raspberry Pi can be useful on its own without the IP address part. Imagine connecting sensors to check the temperature of a room and send an email when the value drops under a certain threshold. Or you can use a Raspberry Pi camera with motion detection software sends out alerts via mail.
Gmail 2-step-verification warning
If you use the 2-step-verification feature to secure your Google account then there are some additional steps you need to take. If you don’t use this feature you can skip this whole section.
In order for SSMTP to be able to communicate with a Gmail account that uses 2-step-verification you will need to create something called an “App Password”. This is a special password that gives SSMTP permission to access your Gmail account without the need of the 2-step-verification. You can get more information by clicking following link.
Make sure to note down the generated app password. You will need it later.
Setting up email
Our plan is to install and configure ssmtp and mailutils, two standard software packages. The first is a MTA (Mail Transport Agent) and will deliver mails to Gmail, while the other one is a well established mail framework.
Start by opening a terminal window and dropping into a root shell.
Update the list of available packages from the package repositories.
Install both ssmtp and mailutils.
apt-get install ssmtp apt-get install mailutils
Edit the SSMTP configuration file using your favorite editor. I prefer nano.
Make changes to the configuration by replacing the settings with your user and password. If you have 2-step-verification enabled please make sure to use the app password instead of your account password.
# The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. email@example.com # The place where the mail goes. mailhub=smtp.gmail.com:587 # Account information AuthUserfirstname.lastname@example.org AuthPass=your_password AuthMethod=LOGIN # Secure the communication UseTLS=Yes UseSTARTTLS=Yes
Edit the SSMTP aliases file to associate local users with Gmail accounts.
Type in the following configuration.
If you use a different username to log in to your Raspberry Pi make sure you use the correct one above.
We are now done with the setup and configuration part. Let’s see if everything works by sending out a test mail.
echo "Test text" | mail -s "Test Mail" email@example.com
Mailing the IP
In order to send out the IP address of the Raspberry Pi we will create a script. By placing this script in the /etc/network/if-up.d folder it will be called automatically every time a network interface gets into UP state.
Open up a terminal and create a script called mailip. If you choose to use a different name for your script make sure to omit the extension.
sudo nano /etc/network/if-up.d/mailip
Type in the following content replacing the email address at the end of the file with your own.
#!/bin/sh # Send a mail with the IP address after interface comes up # It is safe to ignore localhost if [ "$IFACE" = lo ]; then exit 0 fi # Only run from ifup. if [ "$MODE" != start ]; then exit 0 fi # We only care about IPv4 and IPv6 case $ADDRFAM in inet|inet6|NetworkManager) ;; *) exit 0 ;; esac # We wait for DHCP to assign an IP address sleep 15 # Store the IP address to a variable MYIP="$(/bin/hostname --all-ip-addresses)" # Send the mail if the address is not empty if [ -z "$MYIP" ]; then exit 0 else echo "$MYIP" | /usr/bin/mail -s "PI is up" firstname.lastname@example.org fi exit 0
The last step we need to do is to make the above script executable
sudo chmod +x /etc/network/if-up.d/mailip
This is it, we are done. You can now restart your Raspberry PI to test if everything works. Check your Gmail Inbox, you should receive an email with the IP address of the device.