1. 程式人生 > >java 利用jsch埠轉發 建立連線

java 利用jsch埠轉發 建立連線

其他程式碼都一樣,參照《 使用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; }