Last active
August 29, 2015 13:59
-
-
Save itsjustcon/10792550 to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/bash | |
# INSTALL: sudo wget <RAW link from gist here> -O /usr/bin/pifinder; chmod 755 /usr/bin/pifinder; | |
# USAGE: pifinder | |
# pifinder 192.168.1.* | |
# pifinder 192.168.*.* | |
# RPi Connect function | |
connect () { | |
echo "Connecting to Raspberry Pi at $1 now..." | |
ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no pi@$1 | |
} | |
# Get sudo if not | |
sudo ls 1> /dev/null | |
# Get subnet to scan | |
if [ $1 ]; then | |
subnet=$1 | |
else | |
for line in $( ifconfig | grep -o "^[a-z0-9]\+" ); do | |
_ip=`ipconfig getifaddr $line` | |
if [ $_ip ]; then | |
subnet=`echo $_ip | egrep -o "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"`".*" | |
break | |
fi | |
done | |
fi | |
# Search for Pi on subnet | |
if [[ ! -z "$subnet" ]]; then | |
echo "Scanning subnet: $subnet" | |
IPaddr=`sudo nmap -sP $subnet | grep -B 2 -e "(Raspberry Pi Foundation)" | egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"` | |
else | |
echo "ERROR: Could not find subnet to scan!" | |
echo "Try $0 xxx.xxx.xxx.* where xxx.xxx.xxx is your subnet." | |
exit | |
fi | |
# Split all found RPi IP addresses into an array | |
addrs=() | |
while read -r addr; do | |
addrs+=("$addr") | |
done <<< "$IPaddr" | |
if [[ ${#addrs[@]} = 0 ]]; then | |
echo "ERROR: Couldn't find a Raspberry Pi on subnet: $subnet" | |
exit | |
elif [[ ${#addrs[@]} = 1 ]]; then | |
# Connect to Pi at ${#addrs[0]} | |
echo "Found one pi at ${#addrs[0]}" | |
else | |
echo "Found ${#addrs[@]} Raspberry Pi's on subnet: $subnet" | |
select addr in "${addrs[@]}" | |
do | |
if [[ -z "$addr" ]]; then | |
echo "Invalid selection." | |
continue | |
else | |
IP="$addr" | |
break | |
fi | |
done | |
fi | |
# Connect to RPi at $IP | |
connect "$IP" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment