network-locations.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env bash
  2. # mac-ops :: network-locations.sh
  3. # Inventory macOS Network Locations (System Settings → Network → ... → Locations).
  4. # Each location is a separate set of network preferences — useful for "home vs
  5. # office vs cafe" profiles. A stale location may have wrong DNS or proxy.
  6. set -u
  7. while [[ $# -gt 0 ]]; do
  8. case "$1" in
  9. --help|-h)
  10. cat <<EOF
  11. Usage: $0 [options]
  12. --json, --redact, --quiet, --verbose
  13. Reports:
  14. 1. Configured network locations + active location
  15. 2. Per-location DNS / proxy / search-domain config
  16. 3. Stale locations referencing missing network services
  17. 4. Network service order (which interface "wins" for the default route)
  18. Switch location:
  19. System Settings → Network → ... → Locations → choose
  20. Or CLI: networksetup -switchtolocation "Location Name"
  21. EOF
  22. exit 0 ;;
  23. *) shift ;;
  24. esac
  25. done
  26. source "$(dirname "$0")/_lib/common.sh"
  27. parse_common_flags "$@"
  28. maybe_filter_self "$@"
  29. # ----------------------------------------------------------------------------
  30. section "1. CONFIGURED LOCATIONS"
  31. # ----------------------------------------------------------------------------
  32. current=$(networksetup -getcurrentlocation 2>/dev/null)
  33. locations=$(networksetup -listlocations 2>/dev/null)
  34. loc_count=$(echo "$locations" | grep -c . 2>/dev/null || echo 0)
  35. log_info "Network locations configured" "$loc_count"
  36. note " Current location: $current"
  37. note " All locations:"
  38. echo "$locations" | sed 's/^/ /'
  39. # ----------------------------------------------------------------------------
  40. section "2. NETWORK SERVICE ORDER"
  41. # ----------------------------------------------------------------------------
  42. note " Priority order (highest first — first reachable wins default route):"
  43. networksetup -listnetworkserviceorder 2>/dev/null | head -20 | sed 's/^/ /'
  44. # ----------------------------------------------------------------------------
  45. section "3. ACTIVE LOCATION DNS / PROXY STATE"
  46. # ----------------------------------------------------------------------------
  47. note " Per-service DNS:"
  48. networksetup -listallnetworkservices 2>/dev/null | tail -n +2 | while read -r svc; do
  49. [[ "$svc" == \** ]] && continue # disabled
  50. dns=$(networksetup -getdnsservers "$svc" 2>/dev/null)
  51. [[ "$dns" == *"aren't any"* ]] && dns="(none)"
  52. printf " %-35s %s\n" "$svc:" "$dns"
  53. done
  54. note ""
  55. note " Per-service search domains:"
  56. networksetup -listallnetworkservices 2>/dev/null | tail -n +2 | while read -r svc; do
  57. [[ "$svc" == \** ]] && continue
  58. sd=$(networksetup -getsearchdomains "$svc" 2>/dev/null)
  59. [[ "$sd" == *"aren't any"* ]] && continue
  60. printf " %-35s %s\n" "$svc:" "$sd"
  61. done
  62. # Web proxy state
  63. note ""
  64. note " Web proxy state:"
  65. scutil --proxy 2>/dev/null | grep -E "HTTPEnable|HTTPSEnable|ProxyAutoConfigEnable|ProxyAutoConfigURLString" | sed 's/^/ /'
  66. # ----------------------------------------------------------------------------
  67. section "4. NETWORK PREFERENCES PLIST INSPECTION"
  68. # ----------------------------------------------------------------------------
  69. # /Library/Preferences/SystemConfiguration/preferences.plist holds all locations
  70. # We can extract the location list defensively
  71. prefs_plist="/Library/Preferences/SystemConfiguration/preferences.plist"
  72. if [[ -r "$prefs_plist" ]]; then
  73. # Extract just the Sets dict, which has one entry per location
  74. set_count=$(plutil -extract Sets raw -o - "$prefs_plist" 2>/dev/null | wc -l | tr -d ' ')
  75. log_info "preferences.plist Sets count" "$set_count"
  76. fi
  77. # ----------------------------------------------------------------------------
  78. section "5. STALE / DISABLED SERVICES"
  79. # ----------------------------------------------------------------------------
  80. # Asterisked services in listallnetworkservices are disabled
  81. disabled=$(networksetup -listallnetworkservices 2>/dev/null | grep '^\*' || true)
  82. if [[ -n "$disabled" ]]; then
  83. n=$(echo "$disabled" | wc -l | tr -d ' ')
  84. log_info "Disabled network services" "$n"
  85. echo "$disabled" | sed 's/^/ /'
  86. else
  87. log_pass "Disabled network services" "0"
  88. fi
  89. # Services referencing missing hardware
  90. note ""
  91. note " Network services check:"
  92. networksetup -listallhardwareports 2>/dev/null | awk '/Hardware Port|Device/{print}' | head -15 | sed 's/^/ /'
  93. # ----------------------------------------------------------------------------
  94. emit_summary