有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java通过SSHJ迁移到超级用户不工作

我的应用程序是用Java编写的控制台应用程序,我使用SSHJ将命令传输到我拥有的Linux服务器

当我尝试运行此命令时:

sudo su superuser

代码被卡住了,我没有收到错误或响应。我能够传递任何其他Linux命令并获得正确的响应。为什么切换到其他用户不起作用

connection = new ConnectionSSHJ(host,authUser,privateKey);
public static boolean runCommand(ConnectionAdapter connection, String command, SoftAsserter softAsserter) {
        boolean executedSuccessfully = true;
        if (command != null && !command.isEmpty()) {
            System.out.println("In runCommand, Remote command: " + command);
            try {
                ConnectionChannel channel = connection.openChannel("exec");
                channel.setCommand(command);
                channel.setInputStream(null);

                //set a stream for the errors from the remote machine shell so we can "catch" them here to write out to the user in the test report
                ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
               channel.setErrStream(errorStream);

                InputStream inputStream = channel.getInputStream();
                channel.connect();

                StringBuilder commandOutput = new StringBuilder();
                //after connects then the command starts to run - if there is an error on the remote console then it will be in the ErrStream,
                //such as: tail: cannot open ‘/usr/local/personetics/p_home//logs/pserver2.log’ for reading: No such file or directory
                commandOutput.append(IOUtils.readFully(inputStream)); // read the whole input stream
                if (channel.getExitStatus() != 0) {
                    //when a diff returns any results then the test fails, do not want it to fail on this
                    executedSuccessfully = command.contains("diff ");
                }
                softAsserter.assertTrue(executedSuccessfully, "Status of running the command on the remote server, success is 0: " + channel.getExitStatus());

                if (commandOutput.length() > 0) {
                    logger.debug("RemoteCommand output: {}", commandOutput.toString());
                }

                String error = errorStream.toString();
                // TODO: Write non hard coded solution
                // Temp: don't fail tail command when it outputs to stderr
                if (!command.toLowerCase().contains("tail -f") && !error.isEmpty()) {
                    softAsserter.fail("<b></br>The command had an error on the remote server.</br>Command: " + command + "</br>Error: " + error + "</b>");
                    executedSuccessfully = false;
                }

                channel.disconnect();
                System.out.println("DONE");
                softAsserter.assertTrue(executedSuccessfully, "Command executed on the remote server: " + command);
            } catch (Exception | ConnectionFailedException e) {
                executedSuccessfully = false;
                softAsserter.fail("An error occurred while trying to run the following Command on the remote server: " + command + "</br> Error: " + e.toString());
            }
        } else {
            executedSuccessfully = false;
            softAsserter.fail("You need to enter a command to run in the remoteCommand section in the test Excel.");
        }
        return executedSuccessfully;
    }

共 (0) 个答案