[英]“Sudo su - weblogic” via a Java Program?
I am trying to connect my remote unix machine and execute some ssh commands using a java program. 我试图连接我的远程Unix机器并使用Java程序执行一些ssh命令。
connection = new Connection(hostname);
connection.connect();
boolean isAuthenticated = connection.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
Session session = connection.openSession();
session.execCommand("sudo su - weblogic");
Here it needs password again & ofcrs, I can't provide because there is no terminal. 在这里,它需要再次输入密码,因为我没有终端,所以我无法提供。 So created a user.sh file @ my unix user home direcotry (/home/..../bharat) with below content.
因此,用以下内容创建了一个user.sh文件@我的Unix用户主目录(/home/..../bharat)。
echo <mypassword> | sudo -S su - weblogic
sudo -S su - weblogic
but now if I call bash user.sh like below 但是现在如果我像下面那样调用bash user.sh
session.execCommand("bash user.sh");
after logging in with my user in java, it gives below error & could not figure out the resolution for this yet. 用我的用户在Java中登录后,它显示以下错误,并且无法解决该问题。
sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo
Please help :) 请帮忙 :)
The Response of Send("cd /u02/app/oracle/xyz/admin/domains/11.1.1.9/xxxx_xx_xxx_xxx.domain/shared/logs/xxxx"); 发送的响应(“ cd /u02/app/oracle/xyz/admin/domains/11.1.1.9/xxxx_xx_xxx_xxx.domain/shared/logs/xxxx”); is below -
在下面 -
Highlighted Red ==> shows the response, I am getting as of now. 突出显示的红色==>显示了响应,截至目前为止。
Highlighted Blue ==> Expected response. 蓝色突出显示==>预期响应。
Highlighted Green ==> works fine if I send 4 smaller commands by splitting the same command. 如果我通过分割同一条命令发送了4条较小的命令,则突出显示的绿色==>效果很好。
As you and @rkosegi say, su
needs a terminal session for the password. 正如您和@rkosegi所说,
su
需要一个终端会话来输入密码。
It looks like the Ganymed SSH-2 library in the example? 在示例中看起来像Ganymed SSH-2库? This has an option for a shell session.
这有一个shell会话选项。 Clearly you now need to handle reading and writing through stdout and stdin directly though.
显然,您现在需要直接通过stdout和stdin处理读写。
For example, with a couple of methods to keep it simpler: 例如,有两种方法可以使其更简单:
public class SshTerminal {
private Connection connection;
private Session session;
private Reader reader;
private PrintWriter writer;
private String lastResponse;
public SshTerminal(String hostname, String username, String password)
throws JSchException, IOException {
connection = new Connection(hostname);
connection.connect();
boolean isAuthenticated = connection.authenticateWithPassword(username,
password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
session = connection.openSession();
session.requestDumbPTY();
session.startShell();
writer = new PrintWriter(session.getStdin());
reader = new InputStreamReader(session.getStdout());
}
public void send(String command) {
writer.print(command + "\n");
writer.flush();
}
public void waitFor(String expected) throws IOException {
StringBuilder buf = new StringBuilder();
char[] chars = new char[256];
while (buf.indexOf(expected) < 0) {
int length = reader.read(chars);
System.out.print(new String(chars, 0, length));
buf.append(chars, 0, length);
}
int echoEnd = buf.indexOf("\n");
int nextPrompt = buf.lastIndexOf("\n");
if (nextPrompt > echoEnd)
lastResponse = buf.substring(echoEnd + 1, nextPrompt);
else
lastResponse = "";
}
public String getLastResponse() {
return lastResponse;
}
public void disconnect() {
session.close();
connection.close();
}
}
This then worked fine: 然后工作正常:
SshTerminal term = new SshTerminal(host, username, password);
term.waitFor("$ ");
term.send("su -");
term.waitFor("Password: ");
term.send(rootPassword);
term.waitFor("# ");
term.send("ls /root");
term.waitFor("# ");
term.send("cat /file-not-found 2>&1");
term.waitFor("# ");
term.send("cat /var/log/messages");
term.waitFor("# ");
String logFileContent = term.getLastResponse();
term.send("exit");
term.waitFor("$ ");
term.send("exit");
term.disconnect();
String[] lines = logFileContent.split("\n");
for (int i = 0; i < lines.length; i++)
logger.info("Line {} out of {}: {}", i + 1, lines.length, lines[i]);
That includes examples of parsing the lines in a response, and forcing error output through. 这包括解析响应中的行并强制输出错误的示例。
Clearly some of the responses there might be different in your environment. 显然,您的环境中的某些响应可能有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.