"Is there [...] a file linking MAC addresses to IP addresses [...] on Linux?"
Yes, sort of: /proc/net/arp. But of course, that is not a real permanent file.
The standard file for your purpose is /etc/ethers, but you have to create and populate it. Doing it by hand is boring, but writing a command-line to do it for you is fun.
You can either
ping all hosts and parse /proc/net/arp (or arp -n
if not on Linux),
or you can use nmap (sudo apt-get install nmap
?) to scan the local network for you, and give you both the IP and MAC addresses.
The first solution could be something like this :
for i in `seq 1 254`; do
ip=192.168.1.$i
if ping -c 1 -w 1 $ip >/dev/null; then
mac=$(grep "^$ip" /proc/net/arp | awk '{print $4}')
echo "$mac $ip"
fi
done
The nmap solution, which should be faster, could look like this:
sudo nmap -sP -n -oN - 192.168.1.0/24 | \
perl -nle 'if (/ \b ([0-9\.]{7,15}) \b /x) {$ip=$1} elsif (/ ([0-9A-F:]{17}) /i) {print "$1 $ip"}'
To also get the DNS names, you could pipe the output from above through something like
| while read mac ip; do name=$(dig -x $ip +short | grep -v '^;'); [ -n "$name" ] && ip=$name; echo "$mac $ip"; done
(With the nmap solution, you could also remove the -n
option, and adapt the regex to get the name. If you really enjoy regular expressions).