ARP and Bonjour to build list of user devices

Hi,
I want to build a list of the devices that are connected to my network and refresh that list every 10th minute (using an RPi with Raspbian). The host names are obtained by using Bonjour which is easily installed by:

sudo apt-get install libnss-mdns

To build the list manually, I do like this:

  1. Find the broadcast address (I sometimes change subnet, so the address could change and the ARP table has to be refreshed at every run):
ip address show | grep eth0 | grep brd | awk '{print $4}'
  1. Ping the broadcast address to build ARP table:
ping <broadcast address>
  1. Print the desired fields of the ARP table:
arp -a | awk '{print $4 " " $1}'

This produces the desired output which would look something like this:

00:11:22:33:AA:BB iPhone1
00:11:22:33:AA:BC my Laptop
00:11:22:33:AA:BD ?
...

I want to make this process automated so that I get an updated list every 10th minute. Does someone know a good way of doing this? Have tried to script it but it seems I can't ping a stored IP address.

Cheers!
Sam

Hi,

In general, if a command works at the shell prompt, it should work if that same command is put in a script that is run by that same shell. What might be happening here perhaps is that paths when running via crontab might not be what you'd expect.

If you try in your script putting full absolute paths to all binaries and files, does it then work OK via a crontab entry ? That's probably the first thing to try, since if this approach is working for you at the command line, it should (generally speaking) work in a script as well.

Also make sure the shebang line at the top of your script matches the shell you expect to use to run the script (so #!/bin/bash for Bash, for instance), in case there is a difference between your own shell and the system default shell, or the shell the user is running as in cron.

1 Like

Thanks drysdalk. Had some errors when converting the obtained broadcast address. Just replaced step 1 and 2 with:

ping -i eth0 255.255.255.255 -b

Problem solved. :wink: