Skip to content

Instantly share code, notes, and snippets.

@atomgomba
Last active November 12, 2018 15:53
Show Gist options
  • Save atomgomba/19624d5114d432765f9013fa339f7ed9 to your computer and use it in GitHub Desktop.
Save atomgomba/19624d5114d432765f9013fa339f7ed9 to your computer and use it in GitHub Desktop.
Find the IP of a Raspberry Pi (or any computer really) on the network by MAC
#!/bin/bash
#
# Find the IP address of a client by MAC on the same network. Sudo privilege and nmap required.
#
# usage: ./findbymac.sh <MAC_ADDRESS>
#
# The first argument to the script is the MAC address or part of the MAC address.
#
# example: sudo ./findbymac.sh "aa:b8"
#
if [ $# -eq 0 ]
then
echo "usage: $0 <MAC_ADDRESS>"
exit 1
fi
if ! [ -x "$(command -v nmap)" ]
then
echo "Please install nmap"
exit 1
fi
MAC=$1
LOCALIP=$(hostname -I | awk '{print $1}')
MASK=$(echo "$IP" | sed 's/[0-9]\+/0\/24/4')
TARGETIP=$(sudo nmap -sP $MASK | grep -B 2 -i "$MAC" | head -n 1 | awk '{print $5}')
echo $TARGETIP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment