You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过HTML或JS创建带命令的SSH链接启动终端执行远程命令

Great question! Let's break down why your initial approach didn't work and walk through feasible solutions using HTML/JavaScript.

First, the standard ssh:// URI scheme only supports basic connection details like username, host, and port — it doesn't support appending remote commands directly in the URL. That's why <a href='ssh://user@1.2.3.4 "ls"'> fails, but the plain connection link works. The protocol simply isn't designed to carry command parameters.

Can This Be Done? (Yes, With JavaScript + Workarounds)

Pure HTML can't handle this, but we can use JavaScript to build solutions that work across major browsers (Chrome/Safari) and operating systems. Here are two practical approaches:

Approach 1: Generate a Downloadable Shell Script (Cross-OS, No System Setup)

This method creates a clickable button that generates a system-specific shell script. When the user downloads and runs it, their terminal will launch, connect via SSH, and execute the command.

Example Code (macOS + Windows Support)

<button id="runSshCmd">SSH to Server & Run `ls`</button>

<script>
document.getElementById('runSshCmd').addEventListener('click', () => {
  const sshCommand = 'ssh user@1.2.3.4 \'ls\'';
  let fileContent, fileName;

  // Detect OS and create appropriate script
  if (navigator.userAgent.includes('Mac')) {
    // macOS .command file (needs #!/bin/bash header)
    fileContent = `#!/bin/bash\n${sshCommand}\nread -p "Press Enter to close terminal..."`;
    fileName = 'run_ssh_ls.command';
  } else if (navigator.userAgent.includes('Win')) {
    // Windows .bat file
    fileContent = `${sshCommand}\npause`;
    fileName = 'run_ssh_ls.bat';
  } else {
    alert('This feature is only supported on macOS or Windows');
    return;
  }

  // Create and trigger download
  const blob = new Blob([fileContent], { type: 'text/plain' });
  const url = URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.href = url;
  link.download = fileName;
  link.click();
  URL.revokeObjectURL(url);
});
</script>

Notes:

  • For macOS: Users will need to right-click the downloaded .command file and select "Open" (since macOS blocks unsigned scripts by default). After that, Terminal will launch and run the SSH command.
  • For Windows: Users just double-click the .bat file, and PowerShell/Command Prompt will execute the SSH command (assuming SSH is installed and configured).

Approach 2: Custom URI Scheme (One-Click Launch, Requires System Setup)

If you want a true one-click experience, you can register a custom URI scheme on the user's system (e.g., sshcmd://). When clicked in the browser, this scheme triggers a local script that launches the terminal and runs the SSH command.

macOS Setup:

  1. Create an AppleScript (save it as an Application named SSHCommandHandler.app):
on open location theURL
  -- Strip the "sshcmd://" prefix from the URL
  set commandPart to text 9 thru -1 of theURL
  set fullSshCmd to "ssh " & commandPart
  
  tell application "Terminal"
    do script fullSshCmd
    activate
  end tell
end open location
  1. Go to System Preferences > Security & Privacy > Privacy > Automation and allow SSHCommandHandler.app to control Terminal.
  2. Now you can use this link in your HTML:
<a href="sshcmd://user@1.2.3.4 'ls'">SSH & Run `ls`</a>

When clicked, this will launch your custom app, which opens Terminal and runs the SSH command.

Pros & Cons:

  • ✅ One-click experience for users who set it up
  • ❌ Requires manual system configuration (not ideal for public-facing websites)
Final Takeaway

Pure HTML can't achieve your goal due to limitations in the ssh:// URI scheme, but JavaScript gives you workable options. The downloadable script approach is best for general users, while the custom URI scheme is great for internal teams or users you can guide through setup.

内容的提问来源于stack exchange,提问作者hagope

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:44:59