【问题标题】:Plink Freezes After the sudo su - commandsudo su - 命令后 Plink 冻结
【发布时间】:2020-01-10 18:07:54
【问题描述】:

我正在尝试使用 plink.exe 从 Windows 机器远程向 linux 机器发出一些命令,并且它成功执行除了 sudo su -

之外的所有命令

事实上,它也会执行 sudo su - 我可以看到 I now superuser 的输出,但此时它会冻结,因此无法执行任何其他命令。

这是我目前使用的:

./plink.exe -ssh -v -pw myPassHere myUser@myHost "hostname;ls -la;sudo su - ; touch test.me"

我还尝试像这样将 sudo su 命令发送到后台:

 ./plink.exe -ssh -v -pw myPassHere myUser@myHost "hostname;ls -la; sudo su - & touch test.me"

这将执行上述命令,包括 sudo su 但会添加:

logout root
stty: Not a typewriter
stty: Not a typewriter
stty: Not a typewriter

并关闭连接 - 仍然没有执行最后一个 touch 命令

还尝试同时添加 & 和 ;像这样:

./plink.exe -ssh -v -pw myPassHere myUser@myHost "hostname;ls -la;sudo su -&;touch test.me"

这给了我:

ksh: syntax error at line 1 : 'end of file' unexpected

我很确定在发出 sudo su - 命令后,提示符会得到一个结果,这就是它冻结但不知道如何暂停一段时间以允许 sudo su - 命令执行或如何执行的原因避免 shell 期望输出。

【问题讨论】:

    标签: plink


    【解决方案1】:

    您不应该在非交互式上下文中使用sudo su -(这种非交互式是导致“stty: Not a typewriter”错误的原因)。

    只需使用sudo touch test.me

    【讨论】:

    • 我没有管理员密码,所以为了以管理员权限执行命令,我需要使用 sudo su -
    【解决方案2】:

    此方法基于通过命令提示符执行。 首先,确保将 Putty 和 Plink 添加到您的环境变量中。 然后,试试这段代码

    try
                    {
        // To connect using putty/plink
                    String initialConnect = "cmd /c PLINK.exe -batch -i \"<path_to_keytab>\" <username>@<hostname>";
                    //Eg.: String initialConnect = "cmd /c PLINK.exe -batch -i \"C:\\Users\\vsakaray\\Downloads\\private_key.ppk\" myuser@10.42.81.9";
                    Process p = Runtime.getRuntime().exec(initialConnect);
                    /* Get OuputStream */
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream())), true);
                    /* Storing list of commands to execute */
                    String commands[] = { "sudo su user", "cd ~", "pwd" };
                    /* Executing commands */
                    for (String command : commands)
                    {
                        writer.println(command.trim());
                    }
                    Thread.sleep(5000);
                    int value = 0;
                    InputStream std = p.getInputStream();
                    /* Printing the console output */
                    if (std.available() > 0)
                    {
                        System.out.println("STD:");
                        value = std.read();
                        System.out.print((char) value);
                        while (std.available() > 0)
                        {
                            value = std.read();
                            System.out.print((char) value);
                        }
                    }
                    System.out.print("DONE!!!");
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
    

    【讨论】: