Community编程与开发github.com

steipete/ssh-doctor

SSH 诊断工具:远程登录、launchd sshd、预认证关闭、过期会话诊断。

ssh-doctor 是什么?

SSH Doctor 是一款专为 macOS 设计的 SSH 连接诊断工具,能够有效诊断 SSH 连接在认证前关闭、远程登录不可用、或本地/远程 Mac 上的 SSH 配置问题。它通过一系列命令和规则,帮助开发者快速定位问题根源,包括验证本地环回(loopback)测试、检查 sshd 配置、分析日志、处理因 launchd 限制导致的陈旧 sshd-session 进程,以及排查防火墙和监听地址设置。该工具支持 Claude Code、Cursor 等 AI 代理环境,提供非交互式 SSH 测试、基线检查、配置分析、日志诊断和安全、可追溯的令牌复制功能。它强调安全性,不打印敏感信息,并指导用户逐步排查从本地服务到网络层面的问题,最终给出根因分析和关闭报告。

兼容平台~Claude Code✓Codex CLI~Cursor
npx skills add https://github.com/steipete/agent-scripts/tree/main/skills/ssh-doctor

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

SSH Doctor

当 SSH 连接在认证前关闭、远程登录似乎已开启但无法使用、或需要对本地/远程 Mac 的 SSH 进行诊断时使用。

规则

  • 不要打印密钥、令牌、完整环境变量或大范围 grep 输出。
  • 先在本地验证:本地环回失败意味着服务端 sshd/launchd/配置问题;本地环回成功但远程失败意味着网络/防火墙/过滤/监听路径问题。
  • 在修改 /etc/ssh/sshd_config 之前,先报告可疑的配置行。
  • 优先使用非交互式 SSH:
ssh -o RequestTTY=no -o RemoteCommand=none HOST 'hostname; id -un'

基线

hostname; id -un; sw_vers
ipconfig getifaddr en0
ipconfig getifaddr en1 2>/dev/null || true
ipconfig getifaddr en7 2>/dev/null || true
sudo systemsetup -getremotelogin
sudo systemsetup -setremotelogin on
sudo launchctl print system/com.openssh.sshd 2>&1 | head -80
sudo launchctl kickstart -k system/com.openssh.sshd
sudo lsof -nP -iTCP:22 -sTCP:LISTEN
nc -vz 127.0.0.1 22
ssh -4 -F /dev/null -o RequestTTY=no -o RemoteCommand=none [email protected] 'hostname; id -un'

仅在密码回退会导致挂起或提示时使用 BatchMode=yes。

配置

sudo sshd -T 2>&1 | egrep -i '^(allowusers|denyusers|allowgroups|denygroups|listenaddress|maxstartups|logingracetime|usepam|passwordauthentication|pubkeyauthentication|authenticationmethods)'
sudo egrep -n '^[[:space:]]*(AllowUsers|DenyUsers|AllowGroups|DenyGroups|Match|MaxStartups|LoginGraceTime|ListenAddress|AuthenticationMethods|UsePAM|PasswordAuthentication|PubkeyAuthentication)\b' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/* 2>/dev/null || true

可疑项:

  • DenyUsers 匹配了目标用户
  • 限制性 AllowUsers / AllowGroups
  • Match 块意外应用
  • MaxStartups 值过小
  • LoginGraceTime 过小
  • ListenAddress 缺少目标接口

日志

sudo log show --last 30m --predicate 'process == "sshd" OR process == "launchd"' --style compact | tail -160

重要的 Mac 症状:

  • 客户端:kex_exchange_identification: Connection closed by remote host
  • 服务器日志:Could not create new instance of inetd service: 67: Too many processes
  • launchctl print system/com.openssh.sshd: copy count 过高
  • 大量 PID 为 1 的 sshd-session: USER 进程

这意味着 launchd 接受了 TCP 但拒绝生成更多 sshd inetd 副本。

修复陈旧 sshd-session

先检查:

sudo launchctl print system/com.openssh.sshd 2>&1 | egrep 'active count|copy count|state =|last exit code|runs ='
ps -axo pid,ppid,uid,user,state,lstart,etime,comm,args | awk '/sshd-session:/ && !/awk/ {print}'
sudo lsof -nP -c sshd-session -iTCP 2>/dev/null | head -120

如果陈旧会话明显滞留并阻塞新 SSH,通过匹配命令行来终止:

ps -axo pid=,args= | awk '/sshd-session: / && !/awk/ {print $1}' | xargs sudo kill -TERM
sleep 2
ps -axo pid=,args= | awk '/sshd-session: / && !/awk/ {print}'

如果 TERM 留下阻塞进程,在检查所有权和活动 shell 后使用 KILL。

防火墙

仅在环回成功但远程失败时执行:

sudo /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --listapps | grep -i ssh -A2 -B2 || true
sudo pfctl -sr 2>/dev/null | head -80
sudo pfctl -si 2>/dev/null | head -80

同时检查监听地址和目标接口:

ifconfig | awk '/^[a-z0-9]+:/{iface=$1; sub(":","",iface)} iface ~ /^en[0-9]+$/ && /inet / {print iface, $2}'
sudo lsof -nP -iTCP:22 -sTCP:LISTEN

OP Profile 块

如果要求确保 ~/.profile 包含从另一台主机复制的由 Codex 管理的 OP_SERVICE_ACCOUNT_TOKEN:

  • 验证确切的变量和标记,不打印值
  • 仅复制匹配的行/块
  • 通过 chmod 600 的临时文件重定向
  • 绝不回显令牌

存在性检查:

awk 'BEGIN{b=0;e=0;x=0} /BEGIN Codex-managed OP_SERVICE_ACCOUNT_TOKEN/ {b=1} /END Codex-managed OP_SERVICE_ACCOUNT_TOKEN/ {e=1} /^[[:space:]]*(export[[:space:]]+)?OP_SERVICE_ACCOUNT_TOKEN=/ {x=1} END{print "marker_begin", b; print "marker_end", e; print "exact_var", x}' ~/.profile

从远程主机追加:

tmpfile=$(mktemp /tmp/codex-op-token.XXXXXX)
chmod 600 "$tmpfile"
ssh -o RequestTTY=no -o RemoteCommand=none HOST 'awk '\''/^[[:space:]]*(export[[:space:]]+)?OP_SERVICE_ACCOUNT_TOKEN=/ {print; exit}'\'' ~/.profile' > "$tmpfile"
if [ -s "$tmpfile" ]; then
  {
    printf '\n# BEGIN Codex-managed OP_SERVICE_ACCOUNT_TOKEN\n'
    sed -n '1p' "$tmpfile"
    printf '# END Codex-managed OP_SERVICE_ACCOUNT_TOKEN\n'
  } >> ~/.profile
fi
rm -f "$tmpfile"

关闭报告

报告:

  • 根本原因
  • 确切修改的命令
  • 验证输出(根据需要省略敏感信息)
  • 是否应重试远程连接

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

steipete/agent-transcript

GitHub PR/issue agent transcripts: redact, preview, and insert safely.

steipete/beeper

Beeper cache: contact hints, room lookup, WhatsApp/iMessage traces, FTS.

steipete/browser-use

Existing Chrome automation: Chrome plugin first, mcporter fallback.

steipete/clawsweeper-status

ClawSweeper status: URLs, workflow health, active workers, ops snapshot.

steipete/clickclack

ClickClack ops: chat app, Hetzner deploy, DNS/docs/app, Docker rollout.

steipete/cloudflare-registrar

Cloudflare Registrar: domain availability, prices, registration via mcporter.

steipete/codex-debugging

Codex debugging: codex-rs core/tui/exec/cli/app-server/config.

steipete/create-cli

CLI UX/spec: args, flags, help, output, errors, config, dry-run.

steipete/discord-clawd

Discord-backed OpenClaw agent/session relay; not archive search.

steipete/domain-dns-ops

DNS/domain ops: registrars, zones, redirects, DNS/HTTP verify, manager truth.

steipete/frontend-design

Frontend UI: pages, apps, components, polished non-generic design.

steipete/github-author-context

GitHub contributor context: identity, activity, trust, company/team signal.

steipete/github-cache-hygiene

GitHub quota/cache hygiene: gh, ghx, xcache, gitcrawl, mirrors, limits.

steipete/github-deep-review

GitHub deep review: bugs, PRs, best fix, stale-or-real, read code first.

steipete/github-project-triage

GitHub issue/PR triage: queues, CI, blockers, risk, proof, next actions.

steipete/hopper-debugger

Hopper debugging: macOS/iOS binaries, ObjC/Swift symbols, dyld, LLDB.

steipete/instruments-profiling

Instruments/xctrace profiling: macOS/iOS traces, binaries, args, exports.

steipete/mac-maintenance

Mac upkeep: brew update/upgrade, pull clean repos, empty Trash.

steipete/maintainer-orchestrator

Open-source maintainer orchestration: Codex app workers, work recovery, dependencies, vision, releases.

steipete/markdown-converter

Markdown conversion: PDF, Office, HTML, data, OCR, audio, ZIP, YouTube.

相关技能