probe 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env bash
  2. # net-ops :: probe (dispatcher)
  3. # Detects the local OS and runs the matching per-OS probe with the same args.
  4. # Supports --watch=N for continuous monitoring (prints state on change only).
  5. # For Windows targets you reach over SSH, invoke scripts/windows/probe.ps1
  6. # directly via PowerShell (see scripts/ssh-bootstrap.sh for the pattern).
  7. set -eu
  8. here="$(cd "$(dirname "$0")" && pwd)"
  9. # Parse --watch (with optional =N seconds; default 30)
  10. watch_interval=0
  11. clean_args=()
  12. for arg in "$@"; do
  13. case "$arg" in
  14. --watch) watch_interval=30 ;;
  15. --watch=*) watch_interval="${arg#--watch=}" ;;
  16. *) clean_args+=("$arg") ;;
  17. esac
  18. done
  19. # Resolve the per-OS probe script
  20. case "$(uname -s 2>/dev/null)" in
  21. Darwin) target="$here/macos/probe.sh" ;;
  22. Linux) target="$here/linux/probe.sh" ;;
  23. CYGWIN*|MINGW*|MSYS*)
  24. cat >&2 <<EOF
  25. Detected Windows shell environment ($(uname -s)).
  26. Run the PowerShell probe directly:
  27. powershell -NoProfile -File "$here/windows/probe.ps1" $*
  28. EOF
  29. exit 2 ;;
  30. *)
  31. echo "net-ops: unsupported OS '$(uname -s 2>/dev/null)'" >&2
  32. exit 2 ;;
  33. esac
  34. # Continuous watch mode: re-run every N seconds, but only print on state change.
  35. # A "state" is the (pass_count, fail_count, first_fail) tuple.
  36. if [[ "$watch_interval" -gt 0 ]]; then
  37. echo "Watching every ${watch_interval}s — prints on state change only. Ctrl-C to stop." >&2
  38. last_state=""
  39. while true; do
  40. # Run in JSON mode to extract summary cleanly; suppress streaming output
  41. json=$(bash "$target" --json ${clean_args[@]+"${clean_args[@]}"} 2>/dev/null | grep '^{"type":"summary"' | head -1)
  42. state=$(printf '%s' "$json" | tr -d ' ')
  43. ts=$(date '+%Y-%m-%d %H:%M:%S')
  44. if [[ "$state" != "$last_state" ]]; then
  45. if [[ -z "$last_state" ]]; then
  46. printf '[%s] initial state: %s\n' "$ts" "$json"
  47. else
  48. printf '[%s] CHANGED: %s\n' "$ts" "$json"
  49. fi
  50. last_state="$state"
  51. else
  52. printf '[%s] (no change)\n' "$ts"
  53. fi
  54. sleep "$watch_interval"
  55. done
  56. fi
  57. # Default: one-shot execution
  58. exec "$target" ${clean_args[@]+"${clean_args[@]}"}