[英]Best way to script remote SSH commands in Batch (Windows)
我正在寻找批处理脚本,这需要在 Linux 上运行远程 ssh 命令。 我希望输出返回,以便我可以在屏幕上显示它或记录它。
我试过putty.exe -ssh user@host -pw password -m command_run
但它没有在我的屏幕上返回任何内容。
以前有人做过这个吗?
PuTTY 的-m
开关将脚本文件的路径作为参数,而不是command 。
参考: https : //the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-m
因此,您必须将命令 ( command_run
) 保存到纯文本文件(例如c:\\path\\command.txt
)并将其传递给 PuTTY:
putty.exe -ssh user@host -pw password -m c:\path\command.txt
但请注意,您应该使用 Plink(PuTTY 套件中的命令行连接工具)。 它是一个控制台应用程序,因此您可以将其输出重定向到一个文件(使用 PuTTY 无法做到这一点)。
命令行语法是相同的,添加了输出重定向:
plink.exe -ssh user@host -pw password -m c:\path\command.txt > output.txt
请参阅使用命令行连接工具 Plink 。
使用 Plink,您实际上可以直接在其命令行上提供命令:
plink.exe -ssh user@host -pw password command > output.txt
类似问题:
使用 PuTTY 从 Windows 在 Linux 上自动运行命令
从批处理文件中执行 Plink 中的命令
您也可以直接Bash on Ubuntu on Windows
使用Bash on Ubuntu on Windows
。 例如,
bash -c "ssh -t user@computer 'cd /; sudo my-command'"
根据以下 Martin Prikryl 的评论:
-t 启用终端仿真。 您是否需要 sudo 的终端仿真取决于配置(默认情况下您不需要它,而许多发行版会覆盖默认值)。 相反,许多其他命令需要终端仿真。
作为替代选项,您可以安装 OpenSSH http://www.mls-software.com/opensshd.html ,然后只需ssh user@host -pw password -m command_run
编辑:安装时从user2687375响应后,仅选择客户端。 完成此操作后,您应该能够从命令启动 SSH。
然后您可以创建一个 ssh 批处理脚本,例如
ECHO OFF
CLS
:MENU
ECHO.
ECHO ........................
ECHO SSH servers
ECHO ........................
ECHO.
ECHO 1 - Web Server 1
ECHO 2 - Web Server 2
ECHO E - EXIT
ECHO.
SET /P M=Type 1 - 2 then press ENTER:
IF %M%==1 GOTO WEB1
IF %M%==2 GOTO WEB2
IF %M%==E GOTO EOF
REM ------------------------------
REM SSH Server details
REM ------------------------------
:WEB1
CLS
call ssh user@xxx.xxx.xxx.xxx
cmd /k
:WEB2
CLS
call ssh user@xxx.xxx.xxx.xxx
cmd /k
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.