Skip to content

Instantly share code, notes, and snippets.

@ifthenelse
Created April 5, 2026 19:12
Show Gist options
  • Select an option

  • Save ifthenelse/6ca1ac1673d2d92b0adc6090aa882829 to your computer and use it in GitHub Desktop.

Select an option

Save ifthenelse/6ca1ac1673d2d92b0adc6090aa882829 to your computer and use it in GitHub Desktop.
List all the devices in the local network and their local IPs
#!/usr/bin/env zsh
set -e
# --- CONFIG ---
NMAP_BIN=""
BREW_BIN=""
# --- FUNCTIONS ---
function find_nmap() {
if command -v nmap >/dev/null 2>&1; then
NMAP_BIN=$(command -v nmap)
return
fi
echo "nmap not found. Attempting installation..."
if command -v brew >/dev/null 2>&1; then
BREW_BIN=$(command -v brew)
else
echo "Homebrew not found. Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
if [[ -x /opt/homebrew/bin/brew ]]; then
BREW_BIN="/opt/homebrew/bin/brew"
elif [[ -x /usr/local/bin/brew ]]; then
BREW_BIN="/usr/local/bin/brew"
else
echo "Homebrew installation failed. Exiting."
exit 1
fi
fi
echo "Installing nmap..."
$BREW_BIN install nmap
if command -v nmap >/dev/null 2>&1; then
NMAP_BIN=$(command -v nmap)
else
echo "nmap installation failed. Exiting."
exit 1
fi
}
function get_active_interface() {
local iface
iface=$(route get default 2>/dev/null | awk '/interface:/{print $2}')
if [[ -z "$iface" ]]; then
echo "Unable to determine active network interface."
exit 1
fi
echo "$iface"
}
function get_ip() {
local iface=$1
ipconfig getifaddr "$iface" 2>/dev/null
}
function get_subnet() {
local ip=$1
if [[ -z "$ip" ]]; then
echo "Unable to determine IP address."
exit 1
fi
# Assume /24 if netmask parsing is unavailable
echo "${ip%.*}.0/24"
}
# --- MAIN ---
echo "Detecting network..."
find_nmap
INTERFACE=$(get_active_interface)
echo "Active interface: $INTERFACE"
IP_ADDR=$(get_ip "$INTERFACE")
echo "Local IP: $IP_ADDR"
SUBNET=$(get_subnet "$IP_ADDR")
echo "Using subnet: $SUBNET"
echo "Running scan (requires sudo)..."
echo
sudo "$NMAP_BIN" -sn -PR "$SUBNET"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment