Skip to content

Instantly share code, notes, and snippets.

@Martins6
Created October 7, 2025 00:03
Show Gist options
  • Save Martins6/23f6169af28f907a129d76a268e94215 to your computer and use it in GitHub Desktop.
Save Martins6/23f6169af28f907a129d76a268e94215 to your computer and use it in GitHub Desktop.
Finding Raspberry Pi on network (connected to the same WLAN).
#!/bin/bash
# Script to find Raspberry Pi on local network
# Uses network scanning and MAC address detection
echo "πŸ” Finding Raspberry Pi on network..."
echo ""
# Step 1: Detect local IP and network range
echo "[1/4] Detecting local network..."
LOCAL_IP=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null)
if [ -z "$LOCAL_IP" ]; then
echo "❌ Error: Could not detect local IP address"
exit 1
fi
# Extract network prefix (e.g., 192.168.0 from 192.168.0.50)
NETWORK_PREFIX=$(echo $LOCAL_IP | cut -d'.' -f1-3)
echo " Network: ${NETWORK_PREFIX}.0/24"
echo ""
# Step 2: Scan network for active devices
echo "[2/4] Scanning network for devices..."
if ! command -v nmap &> /dev/null; then
echo " Installing nmap..."
brew install nmap
fi
nmap -sn ${NETWORK_PREFIX}.0/24 > /dev/null 2>&1
echo " Scan complete"
echo ""
# Step 3: Look for Raspberry Pi MAC addresses
echo "[3/4] Searching for Raspberry Pi devices..."
# Common Raspberry Pi MAC prefixes:
# b8:27:eb - Raspberry Pi Foundation (older models)
# dc:a6:32 - Raspberry Pi Foundation
# e4:5f:01 - Raspberry Pi Foundation
# d8:3a:dd - Raspberry Pi Foundation (Pi 5)
# 28:cd:c1 - Raspberry Pi Foundation
PI_IPS=$(arp -a | grep -iE "b8:27:eb|dc:a6:32|e4:5f:01|d8:3a:dd|28:cd:c1" | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}" | sort -u)
if [ -z "$PI_IPS" ]; then
echo " ❌ No Raspberry Pi devices found by MAC address"
echo ""
echo "Troubleshooting:"
echo " - Make sure the Pi is powered on"
echo " - Verify WiFi/Ethernet is connected"
echo " - Check your router's connected devices list"
exit 1
fi
echo " Found Raspberry Pi device(s):"
for ip in $PI_IPS; do
MAC=$(arp -a | grep "$ip" | grep -oE "([0-9a-f]{1,2}:){5}[0-9a-f]{1,2}")
echo " β€’ $ip (MAC: $MAC)"
done
echo ""
# Step 4: Test SSH connection to each found Pi
echo "[4/4] Testing SSH connections..."
for ip in $PI_IPS; do
echo " Testing $ip..."
if timeout 3 ssh -o ConnectTimeout=2 -o StrictHostKeyChecking=no -o BatchMode=yes adrielmartins@$ip "echo success" &>/dev/null; then
echo ""
echo "βœ… Successfully connected to Raspberry Pi!"
echo ""
echo " IP Address: $ip"
echo " Username: adrielmartins"
echo ""
echo "Connect with:"
echo " ssh adrielmartins@$ip"
echo ""
exit 0
elif timeout 3 ssh -o ConnectTimeout=2 -o StrictHostKeyChecking=no adrielmartins@$ip "echo success" 2>&1 | grep -q "Permission denied"; then
echo ""
echo "βœ… Found Raspberry Pi at $ip"
echo " (Password authentication required)"
echo ""
echo "Connect with:"
echo " ssh adrielmartins@$ip"
echo ""
exit 0
fi
done
echo ""
echo "⚠️ Found Raspberry Pi device(s) but SSH is not responding"
echo ""
echo "Possible issues:"
echo " - SSH service may not be started yet (wait a few minutes)"
echo " - SSH may not be enabled in Ubuntu Server"
echo " - Try connecting manually: ssh adrielmartins@$(echo $PI_IPS | awk '{print $1}')"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment