Skip to content

Instantly share code, notes, and snippets.

@blesson3
Created December 20, 2021 21:11
Show Gist options
  • Save blesson3/cacdc57c4d7530fd2e35c3231e62c025 to your computer and use it in GitHub Desktop.
Save blesson3/cacdc57c4d7530fd2e35c3231e62c025 to your computer and use it in GitHub Desktop.
relatively quick script to find raspberry pis on your current network; built for macos, requires root
#!/usr/bin/env bash
# Find RaspberryPis on your network.
#
# Usage: ./find-pis.sh <iface:default en0>
#
# raspberry pi mac addresses
# source: https://maclookup.app/search/vendors/results?vendor=Raspberry+Pi
# - https://maclookup.app/vendors/raspberry-pi-foundation
# - https://maclookup.app/vendors/raspberry-pi-trading-ltd
PI_MAC_ADDRESS_LIST="3A:35:41|DC:A6:32|E4:5F:01|B8:27:EB"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
set -ue
# default interface is wifi (en0 on mac)
iface="${1:-en0}"
data=$(ifconfig "$iface"|grep "inet ")
# find the ip address and mask of the given iface
ip=$(echo "$data"|cut -f 2 -d " ")
maskHex=$(echo "$data"|cut -f 4 -d " "|sed 's/^0x//')
# convert the hex into an ip address
mask=$(perl -wE "say join('.', map {hex(\$_)} unpack('A2 A2 A2 A2','${maskHex}'))")
# get the cidr from ip and mask
cidr=$(${SCRIPT_DIR}/get-cidr-from-ip-mask.pl "$ip" "$mask")
echo "Searching ${cidr}..."
results=$(sudo nmap -sn -n "$cidr" | awk "/^Nmap/{ip=\$NF}/${PI_MAC_ADDRESS_LIST}/{print ip}")
echo
echo "Found:"
echo $results
#!/usr/bin/env perl -w
use strict;
use warnings 'all';
use feature 'say';
use NetAddr::IP;
my $net = NetAddr::IP->new($ARGV[0], $ARGV[1]);
if (!$net)
{
say "bad arguments";
exit;
}
say $net->network;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment