Java 和 SSH:维护连接 [英] Java and SSH: maintaining a connection

查看:28
本文介绍了Java 和 SSH:维护连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:通过一个连接和多个会话或一个持久会话远程控制 ssh 服务器.

Goal: Remote control ssh server with one connection and multiple sessions or one persistent session.

问题 1:我目前使用 sshj 通过 SSH 进行一些远程控制,它运行良好,但我似乎无法正确处理提示.(主机不提供真正的根,只是 sudo -i 所以我需要先登录).问题 2:我下载了 ExpectJ 来处理提示,但是一旦我以 root 身份登录并通过身份验证,我终生无法弄清楚如何维护会话.

Issue 1: I currently use sshj to do some remote control through SSH and it works well but I cant seem to get it to handle prompts correctly. (the host doesnt provide true root, just sudo -i so I need to log in first). Issue 2: I downloaded ExpectJ to handle the prompt but I can't for the life of me figure out how to maintain a session once I have logged in and authenticated as root.

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

The current Hack solution requires that I re-log in every time:

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可以用来期待"密码提示吗?我找不到任何暗示此类控制的文档.

Question 1: can sshj be used to "expect" password prompts? I couldnt find any documentation alluding to that type of control.

问题 2:如何修改上面的 Expect 代码以维护可以多次调用的持久连接?我希望能够在达到以 root 身份进行身份验证的状态后继续进行交互,但是一旦发送初始命令,Spawn 就会始终关闭.

Quetsion 2: How can I modify the above Expect code to maintain a persistent connection that I can make multiple calls to? I want to be able to continue to interact once I have reached the state of authenticating as root but the Spawn always closes once the initial command has been sent.

推荐答案

问题1:sshj可以用来期待"密码提示吗?我找不到任何暗示此类控制的文档.

Question 1: can sshj be used to "expect" password prompts? I couldnt find any documentation alluding to that type of control.

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

Mainly what sshj provides is a handle on the shell or command's I/O streams, and methods to get stuff like the exit status.

问题 2:如何修改上面的 Expect 代码以维护可以多次调用的持久连接?我希望能够在达到以 root 身份进行身份验证的状态后继续进行交互,但是一旦发送初始命令,Spawn 就会始终关闭.

Quetsion 2: How can I modify the above Expect code to maintain a persistent connection that I can make multiple calls to? I want to be able to continue to interact once I have reached the state of authenticating as root but the Spawn always closes once the initial command has been sent.

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

Multiplexing sessions, even concurrent session over a single SSH connection is supported. But note that you can only have one session for one shell/command/subsystem.

您使用哪种 Session,即 Session.Shell(通过 session.startShell)或 Session.Command(通过 session.exec)?在后一种情况下,一旦您执行完命令,会话将意味着关闭,这是预期的行为.

Which kind of Session are you using, i.e. Session.Shell (via session.startShell) or Session.Command (via session.exec)? In the latter case, once you're done executing a command, the session is meant to close and this is the expected behavior.

通常不会推荐 Shell 而不是 Command,但由于您配备了 ExpectJ,我想它可以处理提示等问题,您可能想要走那条路.如果出现字符回显等问题,请使用 PTY 选项(而不是 session.allocateDefaultPTY 调用 session.allocatePTY 需要一大堆参数,请查看 allocateDefaultPTY 的来源).

Normally wouldn't recommend Shell over Command but since you are equipped with ExpectJ which I guess can deal with prompts and such, you might want to go that route. If there are problems with echoing of characters or such, play with the PTY options (instead of session.allocateDefaultPTY call session.allocatePTY which takes a whole bunch of args, have a look at the source for allocateDefaultPTY).

代码示例:Shell, 命令

此外,如果您将问题缩小到 sshj 的错误,请务必在问题跟踪器上报告 :)

Also, if you narrow the problem down to a bug with sshj, do report it on the issue tracker :)

这篇关于Java 和 SSH:维护连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆