Aug 102012

A week or two ago, the illustrious Jim Manley ran a Raspberry Jam – a meeting of people interested in the Pi. Unfortunately someone walked off with his Raspberry Pi. A bunch of us were chewing over ideas, on the Raspberry Pi forums, for how to add security features to make it possible to locate a stolen Pi.

The whole thing reminded me of a fabulous book I used to love as a kid: The Great Pie Robbery by Richard Scarry
Great Pie Robbery Except the pies in the book were cherry, not raspberry.

Someone else (rurwin) suggested getting your Pi to ‘call home’ by email using a Googlemail or gmail account.

I decided it would be a fun little programming project to have a crack at this. I’ve been learning Python and thought this would be worthwhile. Actually it was. It taught me several new things I didn’t know how to do before…

  • how to query cpu and network data in linux
  • how to call an external process and use the result in python
  • how to send an email in python
  • how to interface to googlemail

…and also, AndrewS gave me some good programming style tips to clean it up. 8-)

So here’s the code. Feel free to use it, improve it customise it and change it. I take no responsibility for how you use it or what happens.

#!/usr/bin/env python2.7
import smtplib, string, subprocess

# Settings
fromaddr = ''
toaddr = ''

# Googlemail login details
username = 'type your googlemail username here'
password = 'type your googlemail password here'

output_if = subprocess.Popen(['ifconfig'], stdout=subprocess.PIPE).communicate()[0]
output_cpu = open('/proc/cpuinfo', 'r').read()

BODY = string.join((
"From: %s" % fromaddr,
"To: %s" % toaddr,
"Subject: Your RasPi just booted",
), "\r\n")

# send the email
server = smtplib.SMTP('')
server.sendmail(fromaddr, toaddr, BODY)

If you are likely to expose your Pi to other people, it would be a good idea to set up a dedicated googlemail account just for this because if your Pi is stolen, the perp will potentially have access to the googlemail password.

What this program does is, it runs the equivalent of these two command line commands and then emails the output to an email account of your choice…

  1. ifconfig
  2. cat /proc/cpuinfo

ifconfig gives output of mac address, network connections with internal IP addresses and general information about the network status of ethernet, wifi and local loopback connections.

cat /proc/cpuinfo gives you serial number and other cpu related information.

This is what the output looks like…

eth0 Link encap:Ethernet HWaddr b8:47:eb:5b:f3:32
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

lo Link encap:Local Loopback
inet addr: Mask:
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

wlan0 Link encap:Ethernet HWaddr 80:3f:03:42:97:c4
inet addr: Bcast: Mask:
RX packets:105 errors:0 dropped:166 overruns:0 frame:0
TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:10146 (9.9 KiB) TX bytes:1974 (1.9 KiB)

Processor : ARMv6-compatible processor rev 7 (v6l)
BogoMIPS : 898.66
Features : swp half thumb fastmult vfp edsp java tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xb76
CPU revision : 7

Hardware : BCM2708
Revision : 0002
Serial : 00000000527af636

And in the email header you have the IP address of the internet connection the Pi is connected to. The one inside the ([ ]). Obviously I have changed mine for security reasons.

Received: from [] ([])
by with ESMTPS id h42sm4503472eem.5.2012.
(version=TLSv1/SSLv3 cipher=OTHER);
Thu, 09 Aug 2012 08:49:45 -0700 (PDT)


This is very simple as it’s just a python script. If you want it to run automatically on startup, edit /etc/rc.local

sudo nano /etc/rc.local

And add this line just above the end where it says exit 0
python /home/pi/

Download and unzip the file

Either click here

or, from /home/pi




Then should be in your /home/pi folder, then

sudo chmod 755 /home/pi/

Edit to insert your own google account username and password and your from and to addresses.
nano /home/pi/

the values you need to change are in green

Also useful if you want to know the local IP address

I have found this quite useful for finding the IP address on my system when I am running headless (no screen). It sends me the IP address every time I boot up the Pi.

You can run it any time you like

It doesn’t have to be done only at startup. If you want to run the script at any time just type…

python /home/pi/

or if youre already in /home/pi, simply…


I got snippets of code from the following sources…
The google emailing code came from here
The BODY bit came from here

Hope this is useful and educational for you. It certainly was for me. ;)

  18 Responses to “Where’s my Pi? pifind – locate your missing raspberry pi”

  1. Are you aware of the open-source Prey Project, it’s open source, has some nice features, runs “stealth” and you don’t need a dedicated Gmail account.
    Of course you will also need to glue the SD-card in your RPi for obvious reasons ;)

    • Hadn’t heard of that one. Looks good for phones and laptops, but as you said – so easy to remove the SD card, it’s less useful for the Pi.

  2. Thanks, it’s just I’am looking for.

    • You’re welcome. Hope it helps. If you have two level login on Google, you will need to use an application specific password, but best idea would be a dedicated gmail account just for this.

  3. Hi,

    i get an error:

    File “”, line 2
    import smtplib, string, subprocess
    IndentationError: unexpected indent

    any idea ?

    • That’s a weird one. How did you get hold of the file? Has a space slipped into line 2 accidentally?

      • i fixed, but the next an i hope last problem:

        File “”, line 2, in
        import smtplib, string, subprocess
        File “/usr/lib/python2.7/”, line 46, in
        import email.utils
        File “/home/pi/”, line 27, in
        server = smtplib.SMTP(‘’)
        AttributeError: ‘module’ object has no attribute ‘SMTP’

      • I did it! The problem was – the name may not be used ;-)

        Thanks to Alex

  4. Hey Alex,

    I forked your script and extended it to retry until the Pi has an IP and can reach the mail server. The code is up here:

    I also added an Apache License to make it truly open source. If that’s all not okay for you tell me and I’ll take it down again.

    Thanks for the nice work you did.


    • That’s fine Johan. I wouldn’t have published it freely if I was precious about it :)

      Keep us up to date with progress if you develop it further

  5. Ho installato e provato, e funziona egregiamente.

    Ma visto che basta cambiare l Sd per perdere definitivamente il PI ho deciso di disattivarlo.

    Esiste un modo per DISINSTALLARE e ripulire la distro?

    I installed and tried, and works well.

    But since just change the Sd to permanently lose the IP I decided to turn it off.

    Is there a way to UNINSTALL and clean up the distro?

    Thank You.

  6. Hi, i want to run this scrip every hour with crontab, I’ve edited crontab using the command

    crontab -e

    this is how my crontab looks:

    00 * * * * /home/pi/agustin/

    @reboot deluged

    I tested crontab with an other script and worked fine. Also if I run the command:


    works perfectly.

    Am i missing something?

    Kind Regards

    • I would change your crontab line to…
      00 * * * * /usr/bin/python /home/pi/agustin/

      and see if that helps. That’s how I usually run Python scripts from cron. But there are other issues with cron, such as the need to specify absolute file paths for everything, which can cause issues that are very hard to track down.

  7. Hi Alex,

    I really like this code, it’s been really useful, thanks.

    I’ve been trying to figure out how to add the external ip address to it, the output from curl I don’t suppose there is an easy way to do this?



  8. Great script! I had to add sleep 60 as the first line of rc.local to get it working on my Raspberry Pi 3

  9. Hi – great script! Very useful as it can also be used for other SBCs, such as Odroid & Pine. To reduce the requirement of checking the header for the IP address, I’ve added another variable which I then call in the subject line:
    external_IP = subprocess.check_output(“wget -qO- | xargs echo”, shell=True)

    Has anyone managed to get Prey to work on RPi?

    Thanks for this code :)

Leave a Reply to Jacob Cancel reply