macOS Network Diagnostics & Information Tool Bash Script

This Bash script collects detailed network information on a macOS system by querying system utilities (like routeifconfig, and ipconfig) and formatting the output with colored text for easy reading. It reports data such as the default gateway, active interface, IP address, subnet mask, broadcast address, MAC address, DNS servers, public IP, and average ping latency

Click to view script…
#!/bin/bash
# netinfo_detailed.sh - Detailed network information for macOS

# Define colors for output (ANSI escape codes)
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}Gathering default route information...${NC}"

# Get the default gateway and active interface from the routing table.
default_route=$(route -n get default 2>/dev/null)
gateway=$(echo "$default_route" | awk '/gateway/ {print $2}')
iface=$(echo "$default_route" | awk '/interface/ {print $2}')

if [ -z "$iface" ]; then
  echo -e "${RED}Error: No active network interface found. Are you connected to a network?${NC}"
  exit 1
fi

echo -e "${GREEN}Default Interface: ${NC}$iface"
echo -e "${GREEN}Gateway: ${NC}$gateway"

# Get the IP address for the active interface.
ip=$(ipconfig getifaddr "$iface")
echo -e "${GREEN}IP Address: ${NC}$ip"

# Retrieve the subnet mask in hexadecimal from ifconfig.
# Expected format is like: "inet 192.168.1.2 netmask 0xffffff00 broadcast 192.168.1.255"
mask_hex=$(ifconfig "$iface" | awk '/inet / {print $4}')
# Remove any leading "0x" using sed so that we have exactly 8 hex digits.
mask_hex=$(echo "$mask_hex" | sed 's/^0x//')
if [ ${#mask_hex} -ne 8 ]; then
  echo -e "${RED}Unexpected netmask format: $mask_hex${NC}"
  netmask="Unknown"
else
  mask1=$((16#${mask_hex:0:2}))
  mask2=$((16#${mask_hex:2:2}))
  mask3=$((16#${mask_hex:4:2}))
  mask4=$((16#${mask_hex:6:2}))
  netmask="${mask1}.${mask2}.${mask3}.${mask4}"
fi
echo -e "${GREEN}Subnet Mask: ${NC}$netmask"

# Get the broadcast address from ifconfig output.
broadcast=$(ifconfig "$iface" | awk '/broadcast/ {print $4}')
echo -e "${GREEN}Broadcast Address: ${NC}$broadcast"

# Get the MAC (hardware) address of the interface.
mac=$(ifconfig "$iface" | awk '/ether/ {print $2}')
echo -e "${GREEN}MAC Address: ${NC}$mac"

# Retrieve DNS servers from /etc/resolv.conf.
dns_servers=$(grep 'nameserver' /etc/resolv.conf | awk '{print $2}' | tr '\n' ' ')
echo -e "${GREEN}DNS Servers: ${NC}$dns_servers"

# Get the public IP address using an external service.
public_ip=$(curl -s ifconfig.me)
echo -e "${GREEN}Public IP Address: ${NC}$public_ip"

# Perform a ping test to google.com and extract the average latency.
echo -e "${BLUE}Performing ping test to google.com...${NC}"
ping_result=$(ping -c 3 google.com 2>/dev/null)
avg_rtt=$(echo "$ping_result" | tail -1 | awk -F'/' '{print $5}')
if [ -n "$avg_rtt" ]; then
  echo -e "${GREEN}Average Latency (ping): ${NC}${avg_rtt} ms"
else
  echo -e "${RED}Ping test failed.${NC}"
fi

echo -e "${BLUE}Detailed network information gathered successfully.${NC}"