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

如何通过PuTTY/SSH自动化向Unix设备提交交互式命令输入?

解决PuTTY自动化执行交互式命令的问题

Hey Victor, I’ve run into similar headaches automating SSH commands on stripped-down Unix-like devices before—let’s walk through how to fix this properly.

核心问题:PuTTY GUI不适合自动化

Your current PuTTY command isn’t working as expected because the PuTTY GUI client wasn’t built for headless automation. The -pw parameter often fails to bypass the password prompt reliably, and it can’t natively handle the kind of interactive input streams you’re needing (simulating manual keystrokes). Instead, you should use Plink—PuTTY’s command-line SSH companion, which is designed specifically for this sort of automation work.

方案1:用Plink实现密码登录+自动化输入

Plink supports both password authentication via command line and input redirection from a file (to mimic typing commands manually).

执行预定义脚本文件

If you just need to run a set of commands from a file, use the -m parameter like this:

plink.exe -ssh -2 -l username -pw "your_password" hostname -m c:\remote.cmd

This will execute all commands in remote.cmd on the target device without any manual intervention.

模拟交互式人工输入

If the device requires step-by-step input (e.g., confirming a prompt, entering extra data after a command), write all your input (including line breaks to simulate pressing Enter) into a text file, then redirect it to Plink:

  1. Create input_sequence.txt with content like:
    first_command
    yes
    second_command
    exit
    
  2. Run Plink with input redirection:
    plink.exe -ssh -2 -l username -pw "your_password" hostname < c:\input_sequence.txt
    

This behaves exactly like you’re typing each line manually into the SSH session.

方案2:用密钥认证(更安全,推荐)

Storing passwords in plaintext is risky. Instead, use SSH key authentication to skip password prompts entirely:

  1. Generate an SSH key pair using PuTTYgen (save the private key as a .ppk file).
  2. Copy the public key to your target device’s ~/.ssh/authorized_keys file (you may need to create this file if it doesn’t exist—make sure permissions are set to 600).
  3. Use Plink with the private key:
    # Run script file
    plink.exe -ssh -2 -l username -i c:\path\to\your_key.ppk hostname -m c:\remote.cmd
    
    # Or simulate interactive input
    plink.exe -ssh -2 -l username -i c:\path\to\your_key.ppk hostname < c:\input_sequence.txt
    

Linux下的替代方案

If you’re working from Linux, you don’t need PuTTY at all—use the native ssh command:

  • For key authentication (recommended):
    ssh username@hostname -i /path/to/private_key < input_sequence.txt
    
  • If you must use password authentication (less secure), use sshpass:
    sshpass -p "your_password" ssh username@hostname < input_sequence.txt
    

关键注意事项

  • Skip the PuTTY GUI for automation—it’s prone to timing issues and prompt detection failures. Plink is the official, reliable tool for this use case.
  • For minimal Unix-like devices, double-check that SSH key authentication is supported (most do, even without a full Linux system).
  • Test your input file carefully to match the exact prompts the device expects—extra line breaks or typos will break the automation flow.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 13:02:53