ssh-bootstrap.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env bash
  2. # net-ops :: ssh-bootstrap.sh
  3. # Establish an SSH session to any target (Windows / macOS / Linux) using
  4. # password auth via sshpass. Reads password from stdin so it never appears
  5. # in argv / shell history. Auto-detects target OS and emits the right
  6. # invocation pattern for follow-up commands.
  7. #
  8. # Usage:
  9. # echo 'password' | scripts/ssh-bootstrap.sh user@host
  10. # scripts/ssh-bootstrap.sh user@host # interactive prompt
  11. set -euo pipefail
  12. TARGET="${1:-}"
  13. if [[ -z "$TARGET" ]]; then
  14. echo "Usage: $0 user@host" >&2
  15. exit 1
  16. fi
  17. if ! command -v sshpass >/dev/null 2>&1; then
  18. echo "sshpass not found. Install:" >&2
  19. echo " macOS: brew install hudochenkov/sshpass/sshpass" >&2
  20. echo " Linux: apt install sshpass / dnf install sshpass" >&2
  21. exit 1
  22. fi
  23. # Read password — from stdin if piped, else prompt
  24. if [[ -t 0 ]]; then
  25. read -rsp "Password for $TARGET: " PASSWORD
  26. echo
  27. else
  28. read -r PASSWORD
  29. fi
  30. export SSHPASS="$PASSWORD"
  31. # Quick connectivity check (also accepts host key on first contact).
  32. # Use a probe that works on all three: `uname -s` on Unix, fails on cmd.exe
  33. # but succeeds on Windows OpenSSH default shell when it's pwsh/powershell.
  34. echo "Probing $TARGET ..."
  35. PROBE=$(sshpass -e ssh \
  36. -o StrictHostKeyChecking=accept-new \
  37. -o ConnectTimeout=10 \
  38. "$TARGET" \
  39. 'uname -s 2>/dev/null || cmd /c ver 2>nul || ver' 2>&1 | tr -d '\r')
  40. echo " Response: $(echo "$PROBE" | head -3 | tr '\n' ' | ')"
  41. # Detect OS family from probe output
  42. OS=""
  43. case "$PROBE" in
  44. *Darwin*) OS="macos" ;;
  45. *Linux*) OS="linux" ;;
  46. *Microsoft*|*Windows*) OS="windows" ;;
  47. esac
  48. if [[ -z "$OS" ]]; then
  49. echo
  50. echo "Could not auto-detect OS. Treating as unknown — defaulting to bash transport."
  51. OS="unknown"
  52. fi
  53. echo "Detected OS family: $OS"
  54. # Per-OS smoke test
  55. case "$OS" in
  56. windows)
  57. echo
  58. echo "Testing PowerShell -EncodedCommand transport ..."
  59. TEST_PS='Write-Output ("PS ready :: " + $PSVersionTable.PSVersion.ToString())'
  60. B64=$(printf '%s' "$TEST_PS" | iconv -t UTF-16LE | base64)
  61. sshpass -e ssh "$TARGET" "powershell -NoProfile -EncodedCommand $B64" 2>&1 | tail -3
  62. ;;
  63. macos|linux|unknown)
  64. echo
  65. echo "Testing bash transport ..."
  66. sshpass -e ssh "$TARGET" 'bash -c "echo BASH_OK :: \$(bash --version | head -1)"' 2>&1 | tail -2
  67. ;;
  68. esac
  69. # Per-OS invocation hints
  70. echo
  71. echo "---"
  72. case "$OS" in
  73. windows)
  74. cat <<EOF
  75. Ready (Windows target). Run a PowerShell script via:
  76. PS_SCRIPT=\$(cat skills/net-ops/scripts/windows/probe.ps1)
  77. B64=\$(printf '%s' "\$PS_SCRIPT" | iconv -t UTF-16LE | base64)
  78. SSHPASS='<password>' sshpass -e ssh $TARGET "powershell -NoProfile -EncodedCommand \$B64"
  79. Drilldown scripts: nrpt-audit.ps1, nrpt-clean.ps1
  80. For zero-friction follow-up, install your pubkey on the target:
  81. Windows admin path: %ProgramData%\\ssh\\administrators_authorized_keys
  82. Windows user path: %USERPROFILE%\\.ssh\\authorized_keys
  83. EOF
  84. ;;
  85. macos)
  86. cat <<EOF
  87. Ready (macOS target). Run a bash script via:
  88. SSHPASS='<password>' sshpass -e ssh $TARGET 'bash -s' < skills/net-ops/scripts/macos/probe.sh
  89. Drilldown scripts: macos/dns-audit.sh, macos/resolver-clean.sh
  90. Persistent access: ssh-copy-id $TARGET
  91. EOF
  92. ;;
  93. linux)
  94. cat <<EOF
  95. Ready (Linux target). Run a bash script via:
  96. SSHPASS='<password>' sshpass -e ssh $TARGET 'bash -s' < skills/net-ops/scripts/linux/probe.sh
  97. Drilldown scripts: linux/dns-audit.sh, linux/resolved-reset.sh
  98. Persistent access: ssh-copy-id $TARGET
  99. EOF
  100. ;;
  101. *)
  102. echo "Generic SSH ready. Run commands directly."
  103. ;;
  104. esac