使用批处理文件和 PuTTY 在远程服务器上执行 sftp 命令

Execute sftp commands on remote server using batch file and PuTTY

我必须将文件从一台服务器传输到另一台服务器。我使用 PuTTY 登录第一个,然后输入:

sftp -v -oIdentityFile=path username@host 
cd path
put file

一切正常!现在我正在尝试使用批处理文件来完成它。在 .bat 中我有:

putty.exe -ssh host1 -l username1 -pw password1 -m script.txt

script.txt 文件中:

sftp -v -oIdentityFile=path username2@host2
cd path
put file
exit

它连接到二号服务器,但随后停止。前缀 sftp> 没有出现,也没有读取以下行。你有什么建议吗?

遥控器shell接过指令,一一执行。所以它执行 sftp,等待它退出(它从不执行)然后才执行 cd 命令(但在 shell 中,而不是在 sftp)、put(失败,因为那不是 shell 命令),等等


如果您的目的是模拟在终端上键入命令,请使用 Plink 和输入重定向。

Plink (PuTTY command-line connection tool) 是 PuTTY 包中的一个工具,其工作方式与 PuTTY 类似,但它是一个控制台应用程序,而不是 GUI 应用程序。因此它可以使用 input/output 重定向。无论如何,Plink 是自动执行任务的工具,而不是 PuTTY。

plink.exe -ssh host1 -l username1 -pw password1 < script.txt

有关详细信息,请参阅超级用户上的 How to type commands in PuTTY by creating batch file?