java 利用jsch端口转发 建立连接

版权声明:无需授权即可转载,甚至无需保留以上版权声明... ... https://blog.csdn.net/qq_28296925/article/details/83057416

其他代码都一样,参照《 使用JSch完成 ssh隧道建立》 只有获取连接的部分不相同。

代码如下:

// 端口转发

// 链接两层

public boolean connect() throws JSchException {

    try {

        session = ssh.getSession(this.username, this.host, this.port);

        session.setPassword(this.password);

        session.setConfig("StrictHostKeyChecking", "no");

        // 堡垒机端口,内部服务器IP ,内部服务器端口

        session.setPortForwardingL(2233, tunnelRemoteHost, 22);

        session.connect(30000);

        setSession(session);

        // todo :此处待判断内部服务器是否启动。

        //在这个链接上打开了一个新的通道

        session.openChannel("direct-tcpip");

        // todo 第一个参数 是堡垒机 还是内部服务器的username

        secondSession = ssh.getSession(this.username, "localhost", 2233);

        secondSession.setPassword(secondPassword);

        secondSession.setConfig("StrictHostKeyChecking", "no");

        // 现在链接搭配了内部服务器中

        secondSession.connect(30000);

        setSecondSession(secondSession);

        setReady(true);

        return true;

    } catch (Exception e) {

        setReady(false);

    }

    return false;

}

猜你喜欢

转载自blog.csdn.net/qq_28296925/article/details/83057416