blog.fasola.ch


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:

http://redirect.fasola.ch/UNIQUENAME/set/LOCAL.IP.HERE

Following that I can be redirected by browsing to http://redirect.fasola.ch/UNIQUENAME  – easy enough.

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

wget -q “redirect.fasola.ch/UNIQUENAME/set/`/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...
#

### BEGIN INIT INFO
# 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.
### END INIT INFO

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

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:

http://redirect.fasola.ch/UNIQUENAME

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…

 

 

Share