通过Java程序"Sudo su - weblogic"?

Bha*_*rat 10 java unix ssh shell weblogic

我正在尝试连接我的远程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");  
Run Code Online (Sandbox Code Playgroud)

这里需要密码和ofcrs,我无法提供,因为没有终端.因此创建了一个user.sh文件@我的unix用户主目录(/home/..../bharat),内容如下.

echo <mypassword> | sudo -S su - weblogic
sudo -S su - weblogic
Run Code Online (Sandbox Code Playgroud)

但现在如果我像下面这样调用bash user.sh

session.execCommand("bash user.sh"); 
Run Code Online (Sandbox Code Playgroud)

在我的用户使用java登录后,它会给出以下错误,但无法确定解决方案.

sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo
Run Code Online (Sandbox Code Playgroud)

请帮忙 :)

发送的响应("cd /u02/app/oracle/xyz/admin/domains/11.1.1.9/xxxx_xx_xxx_xxx.domain/shared/logs/xxxx"); 在下面 -

突出显示的红色==>显示响应,我现在已经到了.

突出显示蓝色==>预期响应.

如果通过拆分相同的命令发送4个较小的命令,突出显示的绿色==>工作正常.

在此输入图像描述

df7*_*899 5

正如你和@rkosegi所说,su需要一个终端会话来获取密码.

它看起来像示例中的Ganymed SSH-2库?这有一个shell会话选项.显然,你现在需要直接通过stdout和stdin来处理读写.

例如,使用几种方法来保持简单:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这很好用:

    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]);
Run Code Online (Sandbox Code Playgroud)

这包括解析响应中的行并强制错误输出的示例.

显然,您的环境中可能会有一些不同的响应.