簡體   English   中英

通過Java程序“ Sudo su-weblogic”?

[英]“Sudo su - weblogic” via 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");  

在這里,它需要再次輸入密碼,因為我沒有終端,所以我無法提供。 因此,用以下內容創建了一個user.sh文件@我的Unix用戶主目錄(/home/..../bharat)。

echo <mypassword> | sudo -S su - weblogic
sudo -S su - weblogic

但是現在如果我像下面那樣調用bash user.sh

session.execCommand("bash user.sh"); 

用我的用戶在Java中登錄后,它顯示以下錯誤,並且無法解決該問題。

sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo

請幫忙 :)

發送的響應(“ cd /u02/app/oracle/xyz/admin/domains/11.1.1.9/xxxx_xx_xxx_xxx.domain/shared/logs/xxxx”); 在下面 -

突出顯示的紅色==>顯示了響應,截至目前為止。

藍色突出顯示==>預期響應。

如果我通過分割同一條命令發送了4條較小的命令,則突出顯示的綠色==>效果很好。

在此處輸入圖片說明

正如您和@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();
    }
}

然后工作正常:

    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]);

這包括解析響應中的行並強制輸出錯誤的示例。

顯然,您的環境中的某些響應可能有所不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM