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;
}
解决方案
推荐阅读
- javascript - 带有连接或大小写的 mysql 条件子查询
- c++ - if-else 语句无法正常工作的奇怪错误
- angular - 如何验证日期并在格式不正确时显示错误
- python - 无法以正确的方式在方法中重用收集的链接
- node.js - 如何在 adobe reader 中使用 nodejs express 在本地打开文件
- laravel - 如何在 Laravel 中更新 Horizon.php 以及如何计算队列中的进程数?
- python - 为什么“pip install”在 Anaconda 3.7 中不起作用?
- reactjs - 无法创建反应,中止安装 yarnpkg add --exact react react-dom react-scripts cra-template 失败
- typescript - 加载 RxJS Typescript 类型而不从 CommonJS 模块导入?
- ios - 如何找到类型的第一个索引