自动将 root 用户密码提供给使用 Plink 从批处理文件执行的“su”

问题描述

我很难像 ssh 密码一样从 root 密码获取输入以自动插入。到目前为止,我有这个,然后它崩溃了。我正在尝试让批处理文件输入 ip、ssh 密码,然后授予 su 访问权限,然后自动输入 root 通行证。任何事情都会有所帮助,谢谢!

color b
cls
echo What is the IP for the unit?:
set /p IP= :

echo What is the SSH Password for this unit?:
set /p Passw0rd= :

echo What is the Root Password for this unit?:
set /p R0ot= :

plink.exe -t -ssh user@%IP% -pw %Passw0rd% -m commands.txt 

exit/b

command.txt 中有什么:

su

解决方法

首先警告:自动化 su,尤其是使用密码的 su,通常是一个坏主意:Allowing automatic command execution as root on Linux using SSH

su 旨在抵制自动化。


无论如何,su 需要输入密码。 plink 将其输入转发到远程命令输入。

所以您需要做的就是将您的变量作为输入传递给 plink(参见 How to pass user input automatically using plink.exe):

echo %R0ot%| plink -t -ssh user@%IP% -pw %Passw0rd% -m command.txt

您也可以直接在 su 命令行上执行 plink,避免需要 command.txt

echo %R0ot%| plink -t -ssh user@%IP% -pw %Passw0rd% su

需要 -t su 通常会拒绝在没有终端仿真的情况下运行。另一方面,-t 可能会给您带来麻烦。例如,对于 -t,您可能只需要在 plink 提示输入密码时才将密码提供给 su。所以你可能需要一个 expect-linke tool。或者使用骇人听闻的解决方案,例如延迟输入,请参阅 Wait between sending login and commands to serial port using Plink


另一个相关问题,显示了略有不同的方法:
Execute sudo command on Linux from plink.exe on Windows