PuTTY 登录后自动执行命令并为其提供输入

Automatically execute a command after PuTTY login and provide it with an input

我在服务器上有一个程序 运行ning。 我需要向该程序发送一些特定的输入。 我尝试使用 PuTTY 做到这一点。

我目前正在使用:

putty.exe -ssh user@server -pw password -m command.txt

其中 command.txt 是包含我尝试在服务器上 运行 命令的文件。 问题是终端在建立连接后立即关闭。 此外,我尝试发送的命令是特定于该程序的,因此它们不是 Linux 命令。它是这样的:

SomeName ENTER
SomePassword ENTER
SomeNumber ENTER

有什么办法吗?

您使用 -m 开关传递的文件只能包含 shell 命令。您不能使用它为这些命令提供输入。为此,必须使用远程 shell 功能,例如输入重定向——如果您真的需要使用 PuTTY。


但一般来说,要自动执行命令,您应该使用 Plink(PuTTY 套件的一部分)。

使用 Plink,您可以这样做:

(
  echo input line 1
  echo input line 2
) | plink.exe -ssh user@example.com -pw password command

或等效项:

plink.exe -ssh user@example.com -pw password command < `input.txt`

其中 input.txt 包含命令的输入:

input line 1
input line 2