12

我需要使用 sshj 库通过 ssh 在远程服务器上执行一些命令序列。

我愿意

        Session session = ssh.startSession();
        Session.Command cmd = session.exec("ls -l");
        System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
        cmd.join(10, TimeUnit.SECONDS);
        Session.Command cmd2 = session.exec("ls -a");
        System.out.println(IOUtils.readFully(cmd2.getInputStream()).toString());

它把我扔了

net.schmizz.sshj.common.SSHRuntimeException: 此会话通道已全部用完

但是我不能为每个命令重新创建会话,因为这个例子将显示主目录列表,而不是 /some/dir 列表。

4

3 回答 3

25

As odd as it is, session can only be used once. So you have to reset the session every time.

    Session session = ssh.startSession();
    Session.Command cmd = session.exec("ls -l");
    System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
    cmd.join(10, TimeUnit.SECONDS);

    session = ssh.startSession();
    Session.Command cmd2 = session.exec("ls -a");
    System.out.println(IOUtils.readFully(cmd2.getInputStream()).toString());

Or if the shell you are connecting to supports delimited commands (and the situation allows it), you can do this (bash example):

session.exec("ls -l; <command 2>; <command 3>");

于 2014-05-05T08:07:36.890 回答
7

您可以考虑使用类似 Expect 的第三方库,它可以简化远程服务的使用和捕获输出。这些库旨在执行一系列命令。您可以尝试以下一组不错的选项:

然而,当我要解决类似的问题时,我发现这些库已经相当老了。它们还引入了许多不需要的依赖项。所以我创建了自己的,并提供给其他人。它被称为ExpectIt。我的图书馆的优点在项目主页上都有说明。你可以试一试。

以下是使用 sshj 与公共远程 SSH 服务交互的示例:

    SSHClient ssh = new SSHClient();
    ...
    ssh.connect("sdf.org");
    ssh.authPassword("new", "");
    Session session = ssh.startSession();
    session.allocateDefaultPTY();
    Shell shell = session.startShell();
    Expect expect = new ExpectBuilder()
            .withOutput(shell.getOutputStream())
            .withInputs(shell.getInputStream(), shell.getErrorStream())
            .build();
    try {
        expect.expect(contains("[RETURN]"));
        expect.sendLine();
        String ipAddress = expect.expect(regexp("Trying (.*)\\.\\.\\.")).group(1);
        System.out.println("Captured IP: " + ipAddress);
        expect.expect(contains("login:"));
        expect.sendLine("new");
        expect.expect(contains("(Y/N)"));
        expect.send("N");
        expect.expect(regexp(": $"));
        expect.send("\b");
        expect.expect(regexp("\\(y\\/n\\)"));
        expect.sendLine("y");
        expect.expect(contains("Would you like to sign the guestbook?"));
        expect.send("n");
        expect.expect(contains("[RETURN]"));
        expect.sendLine();
    } finally {
        session.close();
        ssh.close();
        expect.close();
    }

这是完整可行示例的链接。

于 2014-07-07T07:18:04.420 回答
7

这个问题很老,但只是为了澄清,引用维基https://github.com/hierynomus/sshj/wiki

会话对象不可重用,因此您只能通过 exec()、startShell() 或 startSubsystem() 分别拥有一个命令/shell/子系统。但是您可以通过单个连接启动多个会话。

在我们的例子中,我们已经把它放在一个函数中

public String runCmd(SSHClient sshClient, String command) throws IOException  {
    String response = "";

    try (Session session = sshClient.startSession()) {
        final Command cmd = session.exec(command);
        response = (IOUtils.readFully(cmd.getInputStream()).toString());
        cmd.join(5, TimeUnit.SECONDS);
        // System.out.println("\n** exit status: " + cmd.getExitStatus());
    } 
    return response;
}
于 2018-02-27T15:54:36.050 回答