What's new
  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Script to update the Client List from the arp table

cyruz

Occasional Visitor
Hello guys,

this is a simple script I'm using to automatically populate the Client List in the AiMesh section. This is helpful when using an ASUS device as AP, to avoid manual input and managing names on DHCP side...

Code:
#!/bin/sh
# arp-clientlist
# --------------
# Poll for new clients in the arp table and modify the nvram custom_clientlist variable.
# The format of each entry is <${hostname}>${mac}>0>42>>, where 42 is the ASUS icon.
# This script should be ran through "cru", adding the following line to "services-start":
# [ -x /jffs/scripts/arp-clientlist ] && cru a arp-clientlist "*/10 * * * * /jffs/scripts/arp-clientlist > /dev/null"
# --------------

# Variables
# ---------
DB_FILE="arp-clientlist-db.txt"
TMP_FILE="/tmp/arp-clientlist-string.tmp"
# ---------

# Ensure DB exists.
[ -f "$DB_FILE" ] || touch "$DB_FILE"

# Parse current arp -a.
arp -a | while read -r hostname rest; do
    mac=$(printf "%s\n" "$rest" | awk '{for (i=1;i<=NF;i++) if ($i ~ /:/) {print $i; exit}}')

    if [ -n "$mac" ]; then
        # Convert MAC to uppercase.
        mac=$(printf "%s" "$mac" | tr 'abcdef' 'ABCDEF')

        entry="<${hostname}>${mac}>0>42>>"
        # Check if entry already exists.
        if ! grep -Fxq "$entry" "$DB_FILE"; then
            echo "$entry" >> "$DB_FILE"
        fi
    fi
done

# Build the final string by concatenating everything.
nvram set custom_clientlist=`tr -d '\n' < "$DB_FILE"`
rm -f "$TMP_FILE"

Cheers!
 

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Back
Top