Hi everyone,
I have created a script for myself that would populate both /etc/hosts and /etc/ethers reliably so the Associated Stations listings would always contain at least an IP and a hostname if possible.
This is what it looks like:
Requirements
To use it, it requires the package
arp-scan
to be installed. It will use pretty basic commands, which should be available on even the tiniest of devices, otherwise. The arp-scan-database is not required for this script.
How it works
It uses arp-scan to get a list of stations on your entire network, both LAN and WLAN, to fill up /etc/ethers. It then uses the contents of that file to question your (local) DNS server for hostname information using nslookup.
NOTE: If you don't have a local DNS server on your network, this script will fail to find any FQDN's on the hosts in your network by default. Not having DHCP addresses registered with the local DNS will also make it impossible to resolve anything on an IP, expect the hosts file to remain unchanged if that happens...
Install using the CLI
Copy the following into a file, save it to /root/. I would suggest a name like 'stationinfo.sh', I will be using it in this example. Type 'vi stationinfo.sh' right after logging in. All commands I give should be followed by an enter, obviously.
#!/bin/sh
if [ ! -e "/tmp/etc/ethers" ]; then
if [ -L "/etc/ethers" ]; then
touch /tmp/etc/ethers
else
mv /etc/ethers /tmp/etc/ethers
fi
ln -s /tmp/etc/ethers /etc/ethers
fi
if [ ! -e "/tmp/etc/hosts" ]; then
if [ -L "/etc/hosts" ]; then
touch /tmp/etc/hosts
else
mv /etc/hosts /tmp/etc/hosts
fi
ln -s /tmp/etc/hosts /etc/hosts
fi
# Any custom hosts need to be added here
cat > /etc/hosts <<- EOM
# NOTE: THIS HOSTS FILE WILL BE OVERWRITTEN BY $(readlink -f $0)
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
# Automatically added adresses will be added below:
EOM
LANST=$(ifstatus lan)
LANADDR=`echo "$LANST" | jsonfilter -e '@["ipv4-address"][0].address'`
INTERFACE=`echo "$LANST" | jsonfilter -e '@.device'`
LANNET=`ip r | grep "link\s\+src $LANIP" | awk -F ' ' '{print $1}'`
DNSSERVER=`echo "$LANST" | jsonfilter -e '@["dns-server"][0]'`
arp-scan -q -x -I $INTERFACE $LANNET | awk -F' ' '{print $2 " " $1}' > /etc/ethers
for IPADDR in `cat /etc/ethers | awk -F' ' '{print $2}'`; do
FQDN=`nslookup $IPADDR $DNSSERVER | grep -m 1 arpa | awk -F' ' '{print $4}'`
if [ $FQDN != "find" ]; then
echo "$IPADDR $FQDN" >> /etc/hosts
fi
done
Make it executable by typing 'chmod +x stationinfo.sh'.
Now we need to make it run, every once in a while. Type 'crontab -e' in the CLI. Add the following in the editor:
*/5 * * * * /root/stationinfo.sh
This will make it run every 5 minutes.
You can change it to */15 for every 15 minutes or */30 for every 30 minutes. You can also change */5 to a simple 0, this will make it run every hour, on the hour.
It will need to run once during startup so it can create the necessary files and symlinks so it will not be wearing an unrecoverable hole in your ROM storage. Add the following in the editor after typing 'vi /etc/rc.local':
/root/stationinfo.sh
Make sure you type this above the 'exit 0' line.
The vi editor, daunting if you are not accustomed to it, is surprisingly easy to use, once you know how to. When you open a file it will open it in a 'view' mode. It will accept some commands but in general you will not be able to type or paste anything in the console. Hit the letter 'i' to put it in 'insert mode' to be able to type or paste. Hit Esc to escape to command mode again and in the command mode type ':wq!' to save and quit.
EDIT: UPDATED SCRIPT 6th of March 2023. Making use of ifstatus. Enjoy folks!