December 3, 2012

easily locating a headless device in a LAN

Category: IT – Matt – 1:21 pm

Assume you’ve got a headless device – e.g. a Raspberry Pi – which you want to connect to via the local network, by http, ssh, or whatever protocol.

By default your device will, once plugged to the lan and powered on, get a dynamically assigned ip address. So your first challenge is to find out it’s actual address in order to connect to it. Sure, you can ask your router which address it had assigned to your device or you just try out – or assign it a static ip for the future.

I ruled those options out, because I want to be able to use it in several different, potentially unknown networks – where I might even have no access to the router.

And the last remaining option – just try through the local subnet – isn’t really a straightforward nor elegant way. So I just quickly hacked together a little ‘service’, that allows any device to register its local address and redirects my web browser by a name tag, provided the local network provides access to the internets….

The only thing needed, is to make the device find its own local ip address and register it by requesting a URL:

Following that I can be redirected by browsing to  – easy enough.

The first task is easily done by calling a one-liner from the console:

wget -q “`/sbin/ifconfig eth0 | grep ‘inet addr:’ | cut -d: -f2 | awk ‘{ print $1}’`” >/dev/null 2>&1

To make sure my Raspberry Pi does this by itself after the network’s up, I just registered a little service. Put the following lines into a file called /etc/init.d/register-local-ip

sudo nano /etc/init.d/register-local-ip

(make sure you replace UNIQUENAME with an own name – not too long, but hopefully unique to my picks… 😉

#! /bin/sh
# /etc/init.d/register-local-ip
# register with: sudo update-rc.d register-local-ip defaults
# don't forget to change UNIQUENAME for something more meaningful...

# Provides:          register-local-ip
# Required-Start:    $remote_fs $syslog $network $all
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.

# Carry out specific functions when asked to by the system
case "$1" in
    echo "Starting script register-local-ip "
    wget -q "`/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`" >/dev/null 2>&1
    echo "Stopping script register-local-ip "
#   optional...
    wget -q "" >/dev/null 2>&1
    echo "Usage: /etc/init.d/register-local-ip {start|stop}"
    exit 1

exit 0

make it executable:

sudo chmod +x /etc/init.d/register-local-ip

and register it to start at default runlevels:

sudo update-rc.d register-local-ip defaults

done! no more guessing the local ip. just plug-in your device, wait until it booted, got network access and registered itself. Browse to:

and you’ll be told the local ip and be redirected automatically after 5 seconds.

If you’d like to use the service, feel free…