在批处理中编写远程 SSH 命令脚本的最佳方法 (Windows) [英] Best way to script remote SSH commands in Batch (Windows)
问题描述
我希望批量编写一些脚本,这需要在 Linux 上运行远程 ssh 命令.我希望输出返回,以便我可以在屏幕上显示它或记录它.
I am looking to script something in batch which will need to run remote ssh commands on Linux. I would want the output returned so I can either display it on the screen or log it.
我尝试了 putty.exe -ssh user@host -pw password -m command_run
但它没有在我的屏幕上返回任何内容.
I tried putty.exe -ssh user@host -pw password -m command_run
but it doesn't return anything on my screen.
以前有人这样做过吗?
推荐答案
PuTTY 的 -m
开关将脚本文件的路径作为参数,而不是命令.
The -m
switch of PuTTY takes a path to a script file as an argument, not a command.
参考:https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-m
因此您必须将命令 (command_run
) 保存到纯文本文件(例如 c:pathcommand.txt
)并将其传递给 PuTTY:
So you have to save your command (command_run
) to a plain text file (e.g. c:pathcommand.txt
) and pass that to PuTTY:
putty.exe -ssh user@host -pw password -m c:pathcommand.txt
<小时>
但请注意,您应该使用 Plink(PuTTY 套件中的命令行连接工具).这是一个控制台应用程序,因此您可以将其输出重定向到一个文件(使用 PuTTY 无法做到这一点).
Though note that you should use Plink (a command-line connection tool from PuTTY suite). It's a console application, so you can redirect its output to a file (what you cannot do with PuTTY).
命令行语法相同,添加了输出重定向:
A command-line syntax is identical, an output redirection added:
plink.exe -ssh user@host -pw password -m c:pathcommand.txt > output.txt
使用 Plink,您实际上可以直接在其命令行上提供命令:
And with Plink, you can actually provide the command directly on its command-line:
plink.exe -ssh user@host -pw password command > output.txt
<小时>
类似问题:
使用 PuTTY 从 Windows 在 Linux 上自动运行命令
从批处理文件中在 Plink 中执行命令
这篇关于在批处理中编写远程 SSH 命令脚本的最佳方法 (Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!