[英]It's possible do connect in a server using SSH through other SSH connection in Java?
我的網絡上有兩個服務器(Server1和Server2),我只能通過ssh連接到Server1,我的網絡看不到Server2。
我想制作一個在Server1中進行連接的程序,然后再次使用ssh啟動到Server2的新連接。
我已經使用Java中的SSH庫連接到Server1,但是我不知道如何使用我的實際ssh會話開始新的連接。
就像在終端中執行ssh Server2@10.1.10.10
我怎樣才能做到這一點?
大多數Java SSH API都應該可以做到這一點,因為它只是在連接到第一台主機之后將端口轉發到第二台主機,然后將隧道的輸入/輸出連接到新的客戶端實例中的情況。
但是,使用J2SSH Maverick開源API,我們可以很輕松地為您完成轉發設置。
下面的代碼演示了如何執行此操作,首先打開與第一台服務器的連接。 然后,一旦連接,調用openRemoteClient,它將為第二個返回SshClient對象。 在后台,它使用端口轉發連接第二個客戶端。
該示例使用密碼身份驗證,但是您當然可以使用任何類型的身份驗證。
// Create initial connection
SshConnector con = SshConnector.createInstance();
SshClient ssh = con.connect(new SocketTransport("host.external", 22), "ubuntu");
// Authenticate
PasswordAuthentication pwd = new PasswordAuthentication();
pwd.setPassword("xxxxxxx");
ssh.authenticate(pwd);
// Created forwarded connection
SshClient remoteClient = ssh.openRemoteClient("host.internal", 22, "ubuntu");
// Now you have to authenticate the second client
.....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.