问题描述
我的应用程序是用 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;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)