Raspberry Pi

Raspberry Pi phone home with IP address

Sometimes it’s a pain to keep track of your Raspberry Pi’s IP address. Maybe you want to plug it in on a work or friend’s network without having to plug in a keyboard and mouse. This technique is what I am using to know the IP address of my Raspberry Pi’s no matter how often they change or what the IP address is.

This requires that you have a web server. I’m logging the IP addresses into the web server log files, Apache in my case.

The script source code is here:


ETH0=`/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
WLAN0=`/sbin/ifconfig wlan0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
if [ -n "$ETH0" ]; then
curl ${SERVER}/locater/${HOST}/eth0/${ETH0}
if [ -n "$WLAN0" ]; then
curl ${SERVER}/locater/${HOST}/wlan0/${WLAN0}

Make sure to set “” to a web server name that you own!

I set up a crontab to run this script every 5 minutes:


*/5 * * * * pi /home/pi/bin/

This will drop an entry into your server log files every 5 minutes that looks like this:

[01/Dec/2015:22:25:01 -0500] "GET /locater/pi2b/eth0/ HTTP/1.1" 200 183 "-" "curl/7.26.0"

The useful bit is the location that is requested /locater/pi2b/eth0/
Split the slashes and you get /locater/ then the Raspberry Pi hostname, interface (eth0 or wlan0), and IP address.

I use this technique to keep tabs on the current IP address of nine of my Raspberry Pi computers so I can access them regardless of what IP address they get, or where I plug them in. This also lets me know how many of them are online at any given time since they will phone home every 5 minutes together.

To see the logs, hop on your web server and tail your log file, filtering out the word “locater”. Your log file path may be different, so change /var/log/apache2/access.log to your log file name:

tail -f /var/log/apache2/access.log | grep "locater"

If you want to make the log entry show up as a “HTTP 200 OK” instead of a “HTTP 404 Page Not Found” you need to put a hack on to your web server. This is useful if you run statistics and you do not want a bunch of ugly 404s to show up.

I’m running Apache so I created this directive which will catch any URL request under /locater (since the script fakes subdirectories):

Alias /locater /opt/locater.htm

…with a file at /opt/locater.htm

It doesn’t matter what is in the file, as long as it exists.

2 replies on “Raspberry Pi phone home with IP address”

Hi Chris,

i’m trying this in my environment, but i think that we have missing nay thin in hack to my web server.

the curl command have always this error message

404 Not Found

Not Found
The requested URL /locater/my.local/eth0/ was not found on this server.

Apache/2.2.15 (CentOS) Server at Port 80

the included the alias line in my apache virtual host configuration, and create an empty file named locater.htm

ServerName http://www.teste.local
ServerAlias teste.local
Alias /locater “/var/www/teste/public_html/locater.htm”
DocumentRoot /var/www/teste/public_html
ErrorLog /var/www/teste/error.log
CustomLog /var/www/teste/requests.log common

thanks in advanced.

Sounds like you might not have mod_alias enabled on your Apache. That may be causing the “Alias” directive to not work. List your apache loaded modules and see if that is missing.

Leave a Reply to Nuno Silva Cancel reply

Your email address will not be published. Required fields are marked *