Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;Sudo su-weblogic“;通过Java程序?_Java_Shell_Unix_Ssh_Weblogic - Fatal编程技术网

&引用;Sudo su-weblogic“;通过Java程序?

&引用;Sudo su-weblogic“;通过Java程序?,java,shell,unix,ssh,weblogic,Java,Shell,Unix,Ssh,Weblogic,我正在尝试连接我的远程unix机器,并使用java程序执行一些ssh命令 connection = new Connection(hostname); connection.connect(); boolean isAuthenticated = connection.authenticateWithPassword(username, password); if (isAuthenticated

我正在尝试连接我的远程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");  
这里再次需要密码&ofcrs,我不能提供,因为没有终端。 于是创建了一个user.sh文件@my unix user home direcotry(/home/…/bharat),其中包含以下内容

echo <mypassword> | sudo -S su - weblogic
sudo -S su - weblogic
在用java登录我的用户后,它给出了以下错误&还不能找到解决方法

sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo
请帮忙:)

Send的响应(“cd/u02/app/oracle/xyz/admin/domains/11.1.1.9/xxxx\u xx\u xxx\u 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{
专用连接;
非公开会议;
私人读者;
私人版画作家;
私有字符串响应;
公共SshTerminal(字符串主机名、字符串用户名、字符串密码)
抛出JSCHEException、IOException{
连接=新连接(主机名);
connection.connect();
布尔值isAuthenticated=connection.authenticateWithPassword(用户名,
密码);
如果(isAuthenticated==false)
抛出新IOException(“身份验证失败”);
session=connection.openSession();
session.requestDumbPTY();
session.startShell();
writer=newprintwriter(session.getStdin());
reader=新的InputStreamReader(session.getStdout());
}
公共无效发送(字符串命令){
writer.print(命令+“\n”);
writer.flush();
}
public void waitFor(应为字符串)引发IOException{
StringBuilder buf=新的StringBuilder();
char[]chars=新字符[256];
而(基本指数(预计)<0){
int length=读卡器读取(字符);
系统输出打印(新字符串(字符,0,长度));
追加(字符,0,长度);
}
int-echond=buf.indexOf(“\n”);
int-nextPrompt=buf.lastIndexOf(“\n”);
如果(nextPrompt>echond)
lastResponse=buf.子字符串(echond+1,nextPrompt);
其他的
lastResponse=“”;
}
公共字符串getLastResponse(){
返回最后的响应;
}
公共空间断开连接(){
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]);
SshTerminal term=新的SshTerminal(主机、用户名、密码);
期限。等待时间($);
术语。发送(“su-”);
术语。waitFor(“密码:”);
发送(rootPassword);
术语。等待(“#”);
术语。发送(“ls/root”);
术语。等待(“#”);
term.send(“未找到目录/文件2>&1”);
术语。等待(“#”);
术语发送(“cat/var/log/messages”);
术语。等待(“#”);
字符串logFileContent=term.getLastResponse();
期限。发送(“退出”);
期限。等待时间($);
期限。发送(“退出”);
术语。断开连接();
String[]line=logFileContent.split(“\n”);
对于(int i=0;i
这包括解析响应中的行以及强制执行错误输出的示例


很明显,在您的环境中,有些响应可能会有所不同。

我正在使用java。如何使用ssh-t或-tt?首先,我会阅读您用于ssh的库的文档,也许它可以分配pseudotty?或者将unix用户配置为不需要TTY。@rkosegi如何配置我的用户不使用TTY。是不是什么。。我可以自己做。。或者需要联系unix管理团队?谢谢你的时间和回复。我一定会试试这个,并让你知道。:)先生,你的解决方案很有效。然而,我面临着两个问题。[1] 我想逐行读取,这样我就可以使用log4j将输出的1行打印到日志的1行。[2] “waitFor”方法仅打印输出,而不是在出现错误时打印输出。我尝试了我知道的一切,但都做不到。请帮助男人:)希望答案的更新有解析行和看到错误输出的例子。请注意
waitFor()
中的额外代码位,以存储
lastResponse
。对于错误输出,读取
stdout
stderr
的最简单方法可能是从命令行将
stderr
(流2)重定向到
stdout
(流1)-在示例中查找
2>&1
。也就是说,我不需要做任何特殊的事情来查看这里的错误输出,所以不确定这是否是问题所在。先生,我正在处理一个非常令人厌倦的问题。PFB:我点击“发送”(“cd/u02/app/oracle/xyz/admin/domains/11.1.1.9/xxxx\u xx\u xxx\u xxx.domain/shared/logs/xxxx”);然后每当命令超过特定长度时,wait方法就会在其间打印一些磨损的字符串。听起来像是终端宽度-命令提示符试图滚动命令文本。
session.requestDumbPTY()
默认设置此选项,因此可以直接指定较大的宽度-例如,
session.requestPTY(“dumb”,500,0,0,0,null);
    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]);