Retrieve Network Information on macOS
Collect detailed network information from your MacFleet devices using command-line tools. This tutorial covers MAC address discovery, IP configuration, port monitoring, and network diagnostics for comprehensive fleet management.
Understanding macOS Network Information
macOS provides several command-line utilities for network information gathering:
networksetup
- Hardware port and network configuration managementifconfig
- Network interface configuration and statusnetstat
- Network connections and port informationarp
- Address Resolution Protocol table managementipconfig
- IP address and DHCP configuration
Retrieve MAC Addresses
List All Hardware Ports
#!/bin/bash
# Display all network hardware ports and their MAC addresses
networksetup -listallhardwareports
echo "Hardware port information retrieved successfully"
Get Specific Interface MAC Address
#!/bin/bash
# Get MAC address for specific interface (Wi-Fi)
echo "Wi-Fi Interface (en0) MAC Address:"
networksetup -getmacaddress en0
echo -e "\nEthernet Interface (en1) MAC Address:"
networksetup -getmacaddress en1
echo -e "\nThunderbolt Bridge (bridge0) MAC Address:"
networksetup -getmacaddress bridge0 2>/dev/null || echo "Thunderbolt Bridge not available"
Comprehensive MAC Address Report
#!/bin/bash
# Generate detailed MAC address report
echo "=== MacFleet Network Hardware Report ==="
echo "Device: $(hostname)"
echo "Date: $(date)"
echo "========================================="
# Get all hardware ports
echo -e "\nš” Network Hardware Inventory:"
networksetup -listallhardwareports
echo -e "\nš Primary Interface Details:"
echo "Wi-Fi (en0): $(networksetup -getmacaddress en0 | awk '{print $3}')"
echo "Ethernet (en1): $(networksetup -getmacaddress en1 | awk '{print $3}' 2>/dev/null || echo 'Not available')"
# Check for additional interfaces
echo -e "\nš Additional Interfaces:"
for i in {2..5}; do
mac_addr=$(networksetup -getmacaddress en$i 2>/dev/null | awk '{print $3}')
if [[ -n "$mac_addr" && "$mac_addr" != "not" ]]; then
echo "en$i: $mac_addr"
fi
done
Discover IP Addresses
Basic IP Address Retrieval
#!/bin/bash
# Get IP address for Wi-Fi interface
WIFI_IP=$(ipconfig getifaddr en0 2>/dev/null)
ETHERNET_IP=$(ipconfig getifaddr en1 2>/dev/null)
echo "Network IP Addresses:"
echo "Wi-Fi (en0): ${WIFI_IP:-Not connected}"
echo "Ethernet (en1): ${ETHERNET_IP:-Not connected}"
Comprehensive IP Configuration
#!/bin/bash
# Detailed IP configuration report
echo "=== MacFleet IP Configuration Report ==="
echo "Device: $(hostname)"
echo "Timestamp: $(date)"
echo "========================================"
# Active network interfaces
echo -e "\nš Active Network Interfaces:"
for interface in en0 en1 en2 en3; do
ip_addr=$(ipconfig getifaddr $interface 2>/dev/null)
if [[ -n "$ip_addr" ]]; then
echo "$interface: $ip_addr"
# Get additional details for active interfaces
subnet_mask=$(ipconfig getoption $interface subnet_mask 2>/dev/null)
router=$(ipconfig getoption $interface router 2>/dev/null)
dns_servers=$(ipconfig getoption $interface domain_name_server 2>/dev/null)
echo " Subnet: ${subnet_mask:-N/A}"
echo " Gateway: ${router:-N/A}"
echo " DNS: ${dns_servers:-N/A}"
echo ""
fi
done
# Public IP address
echo "š External IP Address:"
curl -s ifconfig.me 2>/dev/null || echo "Unable to retrieve external IP"
Network Interface Status
#!/bin/bash
# Monitor network interface status
echo "=== Network Interface Status Monitor ==="
# Get interface statistics
echo -e "\nš Interface Statistics:"
ifconfig | grep -E "(en[0-9]:|inet |status:|media:)" | while read line; do
echo "$line"
done
echo -e "\nš DHCP Lease Information:"
for interface in en0 en1; do
lease_info=$(ipconfig getpacket $interface 2>/dev/null)
if [[ -n "$lease_info" ]]; then
echo "Interface $interface:"
echo "$lease_info" | grep -E "(lease_time|server_identifier|domain_name)"
echo ""
fi
done
Analyze Network Configuration
Complete Network Overview
#!/bin/bash
# Comprehensive network configuration analysis
echo "=== Complete Network Overview ==="
ifconfig
echo -e "\nš Network Summary:"
ifconfig | grep -E "^[a-z]" | while read line; do
interface=$(echo $line | cut -d: -f1)
status=$(ifconfig $interface | grep "status:" | cut -d' ' -f2-)
echo "$interface: ${status:-active}"
done
Network Services and DNS
#!/bin/bash
# Network services and DNS configuration
echo "=== Network Services Configuration ==="
echo "š DNS Configuration:"
echo "System DNS Servers:"
scutil --dns | grep "nameserver" | head -5
echo -e "\nš” Network Services:"
networksetup -listallnetworkservices
echo -e "\nš Active Network Service Details:"
active_service=$(networksetup -listallnetworkservices | grep -v "asterisk" | head -2 | tail -1)
if [[ -n "$active_service" ]]; then
echo "Service: $active_service"
networksetup -getinfo "$active_service"
fi
Subnet and Routing Information
#!/bin/bash
# Subnet mask and routing information
echo "=== Routing and Subnet Information ==="
echo "š£ļø Routing Table:"
netstat -rn | head -10
echo -e "\nš Interface Subnet Details:"
for interface in en0 en1; do
ip_addr=$(ipconfig getifaddr $interface 2>/dev/null)
if [[ -n "$ip_addr" ]]; then
subnet_mask=$(ipconfig getoption $interface subnet_mask 2>/dev/null)
echo "$interface: $ip_addr/${subnet_mask:-unknown}"
fi
done
Monitor Port Information
Active TCP Connections
#!/bin/bash
# Display active TCP connections
echo "=== Active TCP Connections ==="
netstat -ap TCP
echo -e "\nš Connection Summary:"
echo "Total connections: $(netstat -ap TCP | grep -c ESTABLISHED)"
echo "Listening ports: $(netstat -ap TCP | grep -c LISTEN)"
echo "Time-wait connections: $(netstat -ap TCP | grep -c TIME_WAIT)"
Listening Ports Analysis
#!/bin/bash
# Analyze listening ports
echo "=== Listening Ports Analysis ==="
echo "š All Listening Ports:"
netstat -a | grep -i "LISTEN"
echo -e "\nš Listening Ports Summary:"
netstat -a | grep -i "LISTEN" | awk '{print $4}' | cut -d. -f2 | sort -n | uniq -c | sort -nr
echo -e "\nš Security-Relevant Ports:"
netstat -a | grep -i "LISTEN" | grep -E ":(22|80|443|993|995|587|25|53|21|23) "
Port Monitoring Script
#!/bin/bash
# Comprehensive port monitoring
LOG_FILE="/var/log/macfleet_ports.log"
monitor_ports() {
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
{
echo "=== Port Monitoring Report ==="
echo "Timestamp: $timestamp"
echo "Device: $(hostname)"
echo "================================"
echo -e "\nš Current Listening Ports:"
netstat -a | grep -i "LISTEN" | head -20
echo -e "\nš Connection Statistics:"
echo "ESTABLISHED: $(netstat -ap TCP | grep -c ESTABLISHED)"
echo "LISTEN: $(netstat -ap TCP | grep -c LISTEN)"
echo "TIME_WAIT: $(netstat -ap TCP | grep -c TIME_WAIT)"
echo "CLOSE_WAIT: $(netstat -ap TCP | grep -c CLOSE_WAIT)"
echo -e "\nšØ Suspicious Connections:"
netstat -ap TCP | grep -E ":(6667|6697|8080|9050|4444|31337)" || echo "None detected"
} | tee -a "$LOG_FILE"
}
# Execute monitoring
monitor_ports
Display ARP Table
Basic ARP Information
#!/bin/bash
# Display Address Resolution Protocol table
arp -a
echo "ARP table retrieved successfully"
Detailed ARP Analysis
#!/bin/bash
# Comprehensive ARP table analysis
echo "=== ARP Table Analysis ==="
echo "Device: $(hostname)"
echo "Date: $(date)"
echo "=========================="
echo -e "\nš Complete ARP Table:"
arp -a
echo -e "\nš ARP Statistics:"
total_entries=$(arp -a | wc -l)
incomplete_entries=$(arp -a | grep -c "incomplete")
complete_entries=$((total_entries - incomplete_entries))
echo "Total ARP entries: $total_entries"
echo "Complete entries: $complete_entries"
echo "Incomplete entries: $incomplete_entries"
echo -e "\nš Network Neighbors:"
arp -a | head -10 | while read line; do
hostname=$(echo $line | cut -d' ' -f1)
ip=$(echo $line | cut -d'(' -f2 | cut -d')' -f1)
mac=$(echo $line | cut -d' ' -f4)
echo "$ip -> $mac ($hostname)"
done
ARP Security Check
#!/bin/bash
# ARP table security analysis
echo "=== ARP Security Analysis ==="
# Check for duplicate MAC addresses (potential ARP spoofing)
echo "š Duplicate MAC Address Check:"
arp -a | awk '{print $4}' | sort | uniq -d | while read mac; do
if [[ -n "$mac" ]]; then
echo "ā ļø Duplicate MAC detected: $mac"
arp -a | grep "$mac"
fi
done
# Check for suspicious patterns
echo -e "\nšØ Security Alerts:"
suspicious_count=$(arp -a | grep -c "incomplete")
if [[ $suspicious_count -gt 10 ]]; then
echo "ā ļø High number of incomplete ARP entries: $suspicious_count"
fi
# Network vendor analysis
echo -e "\nš¢ Network Vendor Analysis:"
arp -a | grep -E "([0-9a-f]{2}:){5}[0-9a-f]{2}" | awk '{print $4}' | cut -d: -f1-3 | sort | uniq -c | sort -nr | head -5
Enterprise Network Monitoring Script
#!/bin/bash
# MacFleet Enterprise Network Monitoring Suite
LOG_FILE="/var/log/macfleet_network.log"
REPORT_FILE="/tmp/network_report_$(date +%Y%m%d_%H%M%S).txt"
# Logging function
log_action() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Generate comprehensive network report
generate_network_report() {
{
echo "MacFleet Network Assessment Report"
echo "Generated: $(date)"
echo "Device: $(hostname)"
echo "User: $(whoami)"
echo "OS Version: $(sw_vers -productVersion)"
echo "=========================================="
echo ""
# Hardware inventory
echo "š§ Network Hardware:"
networksetup -listallhardwareports
echo ""
# IP configuration
echo "š IP Configuration:"
for interface in en0 en1 en2; do
ip_addr=$(ipconfig getifaddr $interface 2>/dev/null)
if [[ -n "$ip_addr" ]]; then
echo "$interface: $ip_addr"
mac_addr=$(networksetup -getmacaddress $interface | awk '{print $3}')
echo " MAC: $mac_addr"
subnet=$(ipconfig getoption $interface subnet_mask 2>/dev/null)
router=$(ipconfig getoption $interface router 2>/dev/null)
echo " Subnet: ${subnet:-N/A}"
echo " Gateway: ${router:-N/A}"
echo ""
fi
done
# Network services
echo "š” Network Services:"
networksetup -listallnetworkservices | grep -v "asterisk"
echo ""
# DNS configuration
echo "š DNS Configuration:"
scutil --dns | grep "nameserver" | head -3
echo ""
# Security assessment
echo "š Security Assessment:"
listening_ports=$(netstat -a | grep -c "LISTEN")
established_connections=$(netstat -ap TCP | grep -c ESTABLISHED)
echo "Listening ports: $listening_ports"
echo "Active connections: $established_connections"
# Check for common security ports
security_ports=$(netstat -a | grep -i "LISTEN" | grep -E ":(22|80|443|993|995)" | wc -l)
echo "Security-relevant ports: $security_ports"
echo ""
echo "Report completed at: $(date)"
} > "$REPORT_FILE"
echo "š Network report generated: $REPORT_FILE"
}
# Network connectivity test
test_connectivity() {
echo "=== Network Connectivity Test ==="
# Test DNS resolution
if nslookup google.com > /dev/null 2>&1; then
echo "ā
DNS resolution: Working"
else
echo "ā DNS resolution: Failed"
fi
# Test internet connectivity
if ping -c 1 8.8.8.8 > /dev/null 2>&1; then
echo "ā
Internet connectivity: Working"
else
echo "ā Internet connectivity: Failed"
fi
# Test local gateway
gateway=$(route -n get default | grep gateway | awk '{print $2}')
if [[ -n "$gateway" ]] && ping -c 1 "$gateway" > /dev/null 2>&1; then
echo "ā
Gateway connectivity: Working ($gateway)"
else
echo "ā Gateway connectivity: Failed"
fi
}
# Main execution
main() {
log_action "=== MacFleet Network Monitoring Started ==="
generate_network_report
echo ""
test_connectivity
log_action "Network monitoring completed. Report: $REPORT_FILE"
}
# Execute main function
main "$@"
Network Information Reference
Common Network Interfaces
Interface | Description | Common Usage |
---|---|---|
en0 | Primary network interface | Wi-Fi connection |
en1 | Secondary interface | Ethernet connection |
en2-en5 | Additional interfaces | USB adapters, Thunderbolt |
lo0 | Loopback interface | Local system communication |
bridge0 | Thunderbolt bridge | Device-to-device connections |
utun0-utun3 | Tunnel interfaces | VPN connections |
Network Commands Quick Reference
# Get interface IP
ipconfig getifaddr en0
# Get interface configuration
ipconfig getoption en0 subnet_mask
ipconfig getoption en0 router
ipconfig getoption en0 domain_name_server
# Network hardware
networksetup -listallhardwareports
networksetup -getmacaddress en0
# Interface status
ifconfig en0
ifconfig -a
# Network connections
netstat -rn # Routing table
netstat -i # Interface statistics
netstat -ap TCP # TCP connections
# ARP operations
arp -a # Show ARP table
arp -d hostname # Delete ARP entry
Advanced Network Diagnostics
Network Performance Testing
#!/bin/bash
# Network performance diagnostics
echo "=== Network Performance Diagnostics ==="
# Interface statistics
echo "š Interface Statistics:"
netstat -i
# Bandwidth monitoring
echo -e "\nš Network Activity (10 seconds):"
if command -v nettop >/dev/null 2>&1; then
timeout 10 nettop -l 1 -J bytes_in,bytes_out -P
else
echo "nettop not available, using netstat"
netstat -i
fi
# Latency testing
echo -e "\nā±ļø Latency Tests:"
echo "Google DNS (8.8.8.8):"
ping -c 3 8.8.8.8 | tail -1
echo "Cloudflare DNS (1.1.1.1):"
ping -c 3 1.1.1.1 | tail -1
Wireless Network Information
#!/bin/bash
# Wireless network detailed information
echo "=== Wireless Network Analysis ==="
# Current Wi-Fi information
current_wifi=$(networksetup -getairportnetwork en0)
echo "Current Wi-Fi: $current_wifi"
# Wi-Fi power status
wifi_power=$(networksetup -getairportpower en0)
echo "Wi-Fi Power: $wifi_power"
# Available networks (requires admin privileges)
echo -e "\nš” Available Networks:"
if [[ $EUID -eq 0 ]]; then
airport -s 2>/dev/null || echo "Airport utility not available"
else
echo "Admin privileges required for network scanning"
fi
# Wi-Fi interface details
echo -e "\nš Wi-Fi Interface Details:"
ifconfig en0 | grep -E "(inet|ether|status)"
Important Notes
- Interface names may vary between macOS versions and hardware
- Administrative privileges required for some network operations
- Security implications - Monitor open ports and connections regularly
- Performance impact - Network monitoring scripts may affect system performance
- Privacy considerations - ARP tables contain information about network neighbors
Troubleshooting
Common Network Issues
No IP Address:
# Renew DHCP lease
sudo ipconfig set en0 DHCP
DNS Resolution Problems:
# Flush DNS cache
sudo dscacheutil -flushcache
Interface Not Responding:
# Reset network interface
sudo ifconfig en0 down
sudo ifconfig en0 up
Remember to test these scripts on individual devices before deploying across your MacFleet environment.