Created
May 16, 2019 06:28
-
-
Save bryanjhv/02a24ceaa501e0e33e7413cd5306e4b3 to your computer and use it in GitHub Desktop.
Pure bash RPi IP address finder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
ifaces=($(ip link | grep '^[0-9]' | cut -d: -f2 | sed '1d;s/ //')) | |
iface=${ifaces[0]} | |
if [ $# -gt 0 ]; then | |
found=0 | |
for i in ${ifaces[@]}; do | |
if [ $1 == $i ]; then | |
found=1 | |
break | |
fi | |
done | |
if [ $found -eq 0 ]; then | |
echo 'ERROR: invalid interface.' | |
exit 1 | |
fi | |
iface=$0 | |
fi | |
ip=$(ip addr show $iface | grep inet | egrep -o '([0-9]+\.?){4}' | sed 2d) | |
subnet=${ip%.*} | |
# TODO: Adjust to network | |
for i in $(seq 100 254); do | |
tp=$subnet.$i | |
ping -w 1 -c 1 $tp &> /dev/null | |
if [ $? -eq 0 ]; then | |
arp=$(arp -n $tp) | |
if [ $? -eq 0 ]; then | |
mac=$(echo "$arp" | sed 1d | awk '{print $3}') | |
if [[ "$mac" =~ ^b8:27:eb ]]; then | |
echo $tp | |
break | |
fi | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment