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:
/home/pi/bin/send-ip-addr.sh
SERVER="http://example.com"
HOST=`/bin/hostname`
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}
fi
if [ -n "$WLAN0" ]; then
curl ${SERVER}/locater/${HOST}/wlan0/${WLAN0}
fi
Make sure to set “http://example.com” to a web server name that you own!
I set up a crontab to run this script every 5 minutes:
/etc/crontab
This will drop an entry into your server log files every 5 minutes that looks like this:
The useful bit is the location that is requested /locater/pi2b/eth0/10.69.1.82
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:
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):
…with a file at /opt/locater.htm
It doesn’t matter what is in the file, as long as it exists.
Leave a Reply