How to check which clients are connected?

Hi, I have a computer that I want to verify is always connected on the wireless network, and send an alert to me if it is disconnected. So, I want to create a cron job to poll the router regularly (say every 5 minutes), grab the list of computers connected to my router, find the computer name and MAC ID within that list, in order to verify that it is indeed connected. The part I am missing is how to get the list of connected computers from the command line, so that I can include that particular code in a shell script. Can you help? Thanks!

@aetherfy - I don't know for sure the best way of doing this, but one idea comes to mind if you're talking about 1 machine (or a reasonably small number of systems)...

Assign a DHCP reservation (sometimes also called static DHCP) for that system's MAC address so that it always has the same IP address. Conversely, if you have a static IP assigned on the machine, no need to do the DHCP reservation. Then create a script that pings the relevant IP address and does whatever is desired based on a positive and/or negative result. Execute that script as a cron job and it should do what you need.

You can look at the current DHCP leases -- at the moment (without a LEDE router in front of me), I can't remember where the file is located, but it is certainly possible to look at this file and search for the relevant MAC. It won't guarantee that it is connected, though, as it is just a list of the active leases.

@aetherfy -

Playing around a bit, I found the other 2 ways you can get this info...

use the 'arp' command -- this will show the MAC address, IP address, and interface (device) for all devices known to the router (used for routing packets -- address resolution protocol is the basis of how each node on the network knows how to get to the next hop).

or

read the file at /tmp/dhcp.leases to get the current list of DHCP leases (dynamic or reserved). This won't have entires for static IP addresses (but arp should).

You still won't know for sure if that device is currently online, just simply that was at some point -- so you'll need to ping or similar in order to verify the online/offline status.

That is the way to go, but as you stated the static leases will not appear there.
This two commands also shows connected wireless clients:

iw wlan0 station dump
iwinfo wlan0 assoclist

You could also do this before arp -a to fill the arp table. You may get many zeroes, to hide them you can use grep -e '0x2', Increase sleep from 1 to 2 or 3 if your device crashes or become unstable.

for ip in `seq 255`; do
    n=$(($n + 1))
    ping -c 1 -t 1 192.168.9.${ip} > /dev/null 2> /dev/null &
    if [ $(( $n % 10 )) == 0 ]; then
        sleep 1
    fi
done

arp -a | grep -e '0x2'
1 Like

The cat /tmp/dhcp.leases will work, since my linux box always gets the same address (despite not being official static lease). So that will work!

...Now I realize no "cron" in busybox. Anything similar to execute shell scripts at regular intervals (1 min, 5 min, 10 min, ex)?

Thanks for all of the suggestions thus far!

There is cron:

cat<<'EOF' >> /etc/crontabs/root
* * * * * /etc/scheduled_tasks.sh >/dev/null 2>&1
EOF

cat<<'EOF' > /etc/crontabs/cron.update
root
EOF

/etc/init.d/cron enable
/etc/init.d/cron start

Please if you finish your script would be so kind of publish it here?

Thanks!