I need the IP address of my Raspberry Pi so that I can SSH into it. This must be possible to do at my university without a monitor connected. So I made a Python script which sends me the IP address by email, but I can't get it to run at boot. I tried adding python3 /home/pi/code/ipmailer.py to the end of /home/pi/.bashrc but I couldn't get it to work. Does anyone know how to get this to work? Or another way to get the IP?

I have a Raspberry Pi 2 model B running Raspbian.

The Python script:

import smtplib from email.mime.text
import MIMEText
import datetime
import subprocess

ip = subprocess.check_output(['hostname', '-I'])
to = 'My email address'
gmail_user = 'Gmail used to send from'
gmail_password = 'Password'

smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.login(gmail_user, gmail_password)

today = datetime.date.today()
my_msg = '%s' % today.strftime('%b %d %Y')
msg = MIMEText(my_msg)
msg['Subject'] = 'Raspberry Pi IP: %s' % ip[:-2].decode("UTF-8")
msg['From'] = gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
  • I can't help with python, but have you tried ping raspberrypi.local or ssh raspberrypi.local `zero-conf`` works on most networks, although university may block.
    – Milliways
    Commented Feb 11, 2016 at 22:52
  • Why not simply use cron and @reboot?
    – Ghanima
    Commented Feb 11, 2016 at 22:54
  • btw: When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. to consider when thinking about when .bashrc is sourced ot not.
    – Ghanima
    Commented Feb 11, 2016 at 22:57
  • What @Ghanima means is the program gets activated at the end of the boot process by cron. Run crontab -e Then add a line like @ reboot sudo python /home/pi/⋯.py
    – Milliways
    Commented Feb 12, 2016 at 3:38
  • Possible duplicate of How do I run a single line of code (with sudo) on boot up?
    – Piotr Kula
    Commented Feb 12, 2016 at 8:17

3 Answers 3


This isn't emailing the IP to you, but it's similar. I saw this earlier today on Raspberry_pi: Decapitating Raspberry Pis with nomohead

His script will have your Pi send the IP address of itself to http://dweet.io, a free service, where you can check it.


Install instructions:

$ git clone https://github.com/hrishioa/nomohead.git
$ wget https://dl.ngrok.com/ngrok_2.0.19_linux_arm.zip
$ unzip ngrok.zip

Go register for ngrok and get an authentication token

$ ./ngrok authtoken <TOKEN HERE>
$ ./setup.sh

Setup asks for the following parameters:

  1. Location of ngrok - enter the directory where you installed ngrok along with ngrok executable (not just the directory)
  2. Dweet ID - This is the ID you will be using to broadcast the IP. Enter something you think is unique (i.e. not raspi)
  3. Dweet ID for tunnel - The first parameter is for the IP. You can use separate IDs for tunnel and IP if you want, if not just press ENTER.
  4. Delay - The ngrok service takes an unpredictable amount of time to initialize (depends on processor and network load), but 1m seems to work fine for me. Increase this value if the ip address shows up but the ngrok tunnel never does. (It is worth pointing out that this is a little redundant. You can simply login to your ngrok account to see open tunnels and connect to them)

Once all values are entered, a cron job is created that runs at boot.

At this point, you can reboot your Pi.

In order to find your Raspberry Pi, you can go to http://dweet.io/follow/[RASPID] or, in order to see all updates in 24 hours, go to http://dweet.io/get/dweets/for/[RASPID] and replace RASPID with the IP or Tunnel ID you gave during Setup. I prefer the former because it looks better, but it is certainly possible to run automated scripts that poll the JSON from the latter and do things once the Raspi comes online.

  • Can you summarize the link in case it goes dead?
    – Human
    Commented Feb 12, 2016 at 6:32
  • He absolutely explains it better in his github: github.com/hrishioa/nomohead but I can try to.
    – acs11e
    Commented Feb 12, 2016 at 6:36
  • In case some one accesses this information from months or years from now, we want the information to be accessible instead of residing on a dead website
    – Human
    Commented Feb 12, 2016 at 7:24
  • This doesnt answer the question I am afraid. The op asked how to execute on boot, as he already made his own solutions. Sorry, I see you are new here but this is a -1
    – Piotr Kula
    Commented Feb 12, 2016 at 8:15

Since .bashrc is invoked first when you login, you need to execute the script earlier. Presuming you are the only user, you could put something like this in your /etc/rc.local before the exit 0 line (replacing YOUR_GMAIL_ADDRESS and YOUR_GMAIL_PASSWORD accordingly):

echo -e "\
Subject: Raspberry Pi IP: $(hostname -I|cut -d" " -f1)\n\
$(date "+%F")"|\
curl -s -n --ssl-reqd --url "smtps://smtp.gmail.com:465" -T - \
--mail-from "YOUR_GMAIL_ADDRESS" \
--mail-rcpt "YOUR_GMAIL_ADDRESS" 

Or as a one-liner:

echo -e "To: YOUR_GMAIL_ADDRESS\nSubject: Raspberry Pi IP: $(hostname -I|cut -d" " -f1)\n$(date "+%F")"|curl -s -n --ssl-reqd --url "smtps://smtp.gmail.com:465" -T - -u YOUR_GMAIL_ADDRESS:YOUR_GMAIL_PASSWD --mail-from "YOUR_GMAIL_ADDRESS" --mail-rcpt "YOUR_GMAIL_ADDRESS"

If you need the external IP, you can replace all instances of $(hostname -I|cut -d" " -f1) with $(dig +short myip.opendns.com @resolver1.opendns.com).

Again, this is if you are the only user since your password will be visible for everyone. To make it a bit safer you could create a file with your credentials in it, readable only by you:

Create a file in your home directory, let's say we name it .gmail_creds. Add your credentials as YOUR_GMAIL_ADDRESS:YOUR_GMAIL_PASSWD, save and close. Set permissions to only readable by you with chmod 0600 .gmail_creds. Then replace



-u "$(cat /home/YOUR_USERNAME/.gmail_creds)"

Note: The cut part in hostname -I|cut -d" " -f1 gives the very first IP address returned, since there can be more than one. The -d" " specifies a single space as the field delimiter, the -f1 returns the first field. It can be safely removed (including the $() part) if not needed.

Good luck!


If you have access to an external php webserver you don't need python.

Warning: untested code

On the Raspberry side:

wget -s -q "http://yourserver/raspberry.php?lan=`ip address list | grep inet | grep -v 127.0.0 | cut -d " " -f 6 | cut -d "/" -f 1`

On the webserver side (raspberry.php);

$file = 'raspberry.txt';
if ($_REQUEST)
    $write[] = 'Raspberry';
    $write[] = 'Last seen '.date('Y-m-d H:i:s');
    $write[] = 'Local IP '.$_REQUEST['lan'];
    $write[] = 'Remote IP '.$_SERVER['REMOTE_ADDR'];
    echo file_get_contents($file);

To get this to be runned at bootup I refer to How do I run a single line of code (with sudo) on boot up?

  • At least you linked to an answer about running at boot :) The OP already has his own solution he was really looking on how to run it on boot. I suppose your link justifies a duplicate.
    – Piotr Kula
    Commented Feb 12, 2016 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.