代码之家  ›  专栏  ›  技术社区  ›  ebt

Java和SSH:维护连接

  •  1
  • ebt  · 技术社区  · 14 年前

    问题1:我目前使用sshj通过SSH进行一些远程控制,它工作得很好,但我似乎无法让它正确处理提示(主机不提供真正的root,只提供sudo-i,所以我需要先登录)。 问题2:我下载了ExpectJ来处理提示,但是我一辈子都不知道如何在以root身份登录和验证之后维护会话。

    当前的黑客解决方案要求我每次重新登录:

    public class Expect {
        Spawn shell;
        ExpectJ exp;
        String host;
        int port;
        String username;
        String passwd;
        boolean sudo = false;
        public Expect(String host,int port,String username,String passwd) throws IOException, TimeoutException, ExpectJException{
    
            exp = new ExpectJ(5);
            this.host = host;
            this.port = port;
            this.username = username;
            this.passwd = passwd;
            shell = exp.spawn(host, port, username, passwd);
            shell.send("sudo netstat -natvp  | grep Xtightvnc\n");
            System.out.println(shell.getCurrentStandardOutContents());
            try{
                shell.expect("[sudo] password for #######:");
                shell.send(passwd+"\n");
            }
            catch (IOException ex){
                String err = ex.toString();
                if(!err.equals("java.io.IOException: End of stream reached, no match found")){
                    throw new IOException(ex);
                }
            }
        }
    

    问题1:sshj能否用于“预期”密码提示?我找不到任何暗示这种控制的文件。

    1 回复  |  直到 14 年前
        1
  •  1
  •   shikhar    14 年前

    问题1:sshj能否用于“预期”密码提示?我找不到任何暗示这种控制的文件。

    sshj提供的主要是shell或命令的I/O流的句柄,以及获取退出状态等信息的方法。

    多路复用会话,甚至支持通过单个SSH连接的并发会话。但是请注意,对于一个shell/命令/子系统,只能有一个会话。

    我的意思是

    source for allocateDefaultPTY ).

    Shell , Command

    另外,如果您将问题缩小为sshj的bug,请在问题跟踪程序中报告:)