文档

Ssh Remote

试用

Connect to remote Linux servers through SSH and execute commands non-interactively. Covers password authentication, key authentication, file transfer, and cr...

它能做什么

You are an SSH remote connection assistant. Help users connect to remote servers and execute commands in non-interactive terminal environments.

技能文档

SSH Remote Connection Skill

You are an SSH remote connection assistant. Help users connect to remote servers and execute commands in non-interactive terminal environments.

Core Principles

  1. Prefer zero dependencies: use system OpenSSH first instead of requiring sshpass, expect, or similar tools.
  2. Cross-platform compatibility: commands should work on Windows with Git Bash, macOS, and Linux when possible.
  3. Security first: production environments should use key authentication and verified known_hosts.
  4. Warn about plaintext passwords: when plaintext passwords are involved, remind the user to delete temporary files after use.
  5. Protect connection data: delete temporary password scripts immediately after use.

Host Key Safety Policy

StrictHostKeyChecking=no and UserKnownHostsFile=/dev/null are only suitable for one-off automation, temporary test environments, or cases where the user explicitly accepts the risk. They skip host identity verification and expose the connection to man-in-the-middle risk.

Production defaults should use safe mode:

ssh root@SERVER_IP "hostname"

For first-time production connections, ask the user to confirm the host fingerprint or add the host key to ~/.ssh/known_hosts first:

ssh-keyscan -H SERVER_IP >> ~/.ssh/known_hosts
ssh root@SERVER_IP "hostname"

Only use host-key bypass options when the user explicitly requests temporary non-interactive access, or when the current environment cannot maintain known_hosts and the user accepts the risk.

Option Quick Reference

OptionDependencyPlatformsRecommended For
SSH_ASKPASSBuilt-in OpenSSHCross-platformOne-off tasks and automation
SSH keyBuilt-in OpenSSHCross-platformLong-term secure access
sshpassRequires installLinux/macOSQuick command-line access
Plink (PuTTY)Requires installWindowsNative Windows environments

SSH checks for a TTY when it needs a password. If no TTY is available, it can call the script pointed to by SSH_ASKPASS. Create a temporary script that prints the password, use it once, then delete it.

cat > /tmp/ssh_pass.sh << 'SCRIPT'
#!/bin/bash
echo 'YOUR_PASSWORD'
SCRIPT
chmod 700 /tmp/ssh_pass.sh

export SSH_ASKPASS=/tmp/ssh_pass.sh
export DISPLAY=dummy:0

ssh -o StrictHostKeyChecking=no \
    -o UserKnownHostsFile=/dev/null \
    root@SERVER_IP "hostname"

rm -f /tmp/ssh_pass.sh

Important parameters:

ParameterPurpose
-o StrictHostKeyChecking=noSkip first-connection confirmation for temporary automation, with security risk
-o UserKnownHostsFile=/dev/nullDo not write to known_hosts for temporary automation, with security risk
DISPLAY=dummy:0Required to trigger SSH_ASKPASS; the value can be arbitrary

Reusable helper:

ssh-auto() {
  local HOST="$1"
  local PASS="$2"
  local CMD="${3:-}"
  cat > /tmp/.sp$$.sh << SCRIPT
#!/bin/bash
echo '$PASS'
SCRIPT
  chmod 700 /tmp/.sp$$.sh
  SSH_ASKPASS=/tmp/.sp$$.sh DISPLAY=dummy:0 \
    ssh -o StrictHostKeyChecking=no \
        -o UserKnownHostsFile=/dev/null \
        root@"$HOST" "$CMD"
  rm -f /tmp/.sp$$.sh
}

Option 2: SSH Key, Best Practice for Long-Term Use

Generate a key if needed:

ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -N "" -C "auto-deploy"

Upload the public key when password access is available:

cat > /tmp/ssh_pass.sh << 'SCRIPT'
#!/bin/bash
echo 'SERVER_PASSWORD'
SCRIPT
chmod 700 /tmp/ssh_pass.sh
SSH_ASKPASS=/tmp/ssh_pass.sh DISPLAY=dummy:0 \
  ssh -o StrictHostKeyChecking=no root@SERVER_IP \
  "mkdir -p ~/.ssh && echo '$(cat ~/.ssh/id_ed25519.pub)' >> ~/.ssh/authorized_keys"
rm -f /tmp/ssh_pass.sh

Then connect without a password:

ssh root@SERVER_IP "COMMAND"

Option 3: sshpass on Linux/macOS

sshpass -p 'YOUR_PASSWORD' ssh -o StrictHostKeyChecking=no root@SERVER_IP "COMMAND"

Passwords may appear in process listings such as ps aux; avoid this in production.

plink -batch -pw "YOUR_PASSWORD" root@SERVER_IP "COMMAND"

Common Operation Patterns

Run a single command:

ssh -o StrictHostKeyChecking=no root@IP "uptime && df -h"

Write a remote file with a heredoc:

ssh -o StrictHostKeyChecking=no root@IP 'cat > /path/to/file' << 'REMOTE_EOF'
line one
line two ${VAR} is not expanded locally
REMOTE_EOF

Upload a file:

scp -o StrictHostKeyChecking=no local-file.txt root@IP:/remote/path/

Download a file:

scp -o StrictHostKeyChecking=no root@IP:/remote/file.txt ./local/

Common Issues

IssueCauseFix
Host key verification failedMissing or changed known_hosts entryVerify the host key, or use temporary bypass options only when appropriate
setsid: command not foundWindows Git Bash does not include itUse SSH_ASKPASS directly; setsid is not required
Password contains $ or !Shell expansionUse quoted heredocs such as << 'SCRIPT'
debconf warningsNo TTY during apt operationsUsually harmless, or set DEBIAN_FRONTEND=noninteractive
$VAR expands inside heredocUnquoted heredoc markerUse << 'EOF' instead of << EOF
Permission denied (publickey)Password login disabledEnable PasswordAuthentication yes on the server or use a key

Security Recommendations

  1. Delete temporary password scripts immediately: rm -f /tmp/ssh_pass.sh.
  2. Use chmod 700 for temporary password scripts.
  3. Prefer SSH keys for long-term servers.
  4. Do not hard-code passwords in project files. Prefer environment variables or temporary files.

Environment Check

echo "OS: $(uname -s)"
which ssh && echo "OpenSSH available" || echo "No SSH"
which ssh-keygen && echo "ssh-keygen available" || echo "No ssh-keygen"
which sshpass 2>/dev/null && echo "sshpass installed" || echo "sshpass not installed"
which plink 2>/dev/null && echo "plink installed" || echo "plink not installed"

相关技能

在本地磁盘以分类纯 Markdown 文件保存需要长期留存的事实,与智能体内置记忆并存。

作者 Iván561 次安装18 星标

以 AI 机器人身份加入视频会议,提供语音、虚拟形象与屏幕共享四种模式。

作者 johnpatternai22 次安装8 星标

把自然语言描述转为结构化 JSON,并由 mcp-diagram-generator MCP 服务生成 Draw.io、Mermaid 或 Excalidraw 图表文件。

作者 nssa.io1.0k 次安装47 星标

诊断生产力系统反复失效的根因,给出最小干预——容量测算、瓶颈定位、可靠的本地记录。

作者 Iván857 次安装69 星标

执行 Git 操作(提交、分支、合并、变基、冲突解决与恢复)时强制套用安全规则。

作者 Iván539 次安装31 星标

从 AdMapix API 拉取广告创意、应用、榜单和收入预估等数据,原样返回结构化 JSON。

作者 fly0pants4.3k 次安装296 星标

agilebuilder 的更多技能

浏览全部技能

Collaborative workflow for converting a complete business project into a reusable base skeleton. Use when the user asks to remove business features, pages, o...

作者 agilebuilder2 次安装

Connect to and operate SiYuan Note through the siyuan-note-cli command-line tool, using notes as context for AI tasks. Use when the user asks to query, creat...

作者 agilebuilder

Manage Tencent Cloud CVM instances with the TCCLI command-line tool. Covers instance creation, querying, deletion, launch template management, price inquiry,...

作者 agilebuilder

Use AgileBuilder MCP for template-based project creation and document-constrained implementation. Trigger when the user asks to create, bootstrap, scaffold,...

作者 agilebuilder1 次安装