I am making some scripts and wanted to know the IP of specific devices (ex. smartphones). To identify the device, the MAC address is enough and a sure way. Some one could use arp-scan
or nmap
to do so, but in my case, i didn't want to use root privileges. I wanted a way, that would be:
- Quick
- No root/sudo usage
- Identify if the device is connected at the time. so
arp -a
can't be used
The solution i found is this. Use ping
to scan local network and find if an IP is active. Filter it through the arp -a
results and match the specific MAC Address i was looking for. This way, i will know for sure that device aa:bb:cc:dd:ee:ff is connected and has the specific IP, which then i can use.
The script is below
#!/usr/bin/bash
arp -a > /tmp/arpa.txt
for ip in $(seq 1 10); do
ping -c 1 192.168.1.$ip -c 1 >/dev/null 2>&1
if [ $? -eq 0 ]; then
cat /tmp/arpa.txt | grep 192.168.1.$ip | grep "$1" >/dev/null 2>&1 && echo "192.168.1.$ip" && break
fi
done
rm /tmp/arpa.txt
It doesn't output anything, except the IP i am looking for. Beware that i have it to scan only the 10 first IPs. Change the value to scan for more IPs. For my network this number is enough, cause i also wanted to be as quickly as possible. When the script, finds the IP, we break the loop, cause there is no need to continue the scan.