簡體   English   中英

Java從現有的打開新的ssh連接

[英]Java open new ssh connection from already existing

我是 JAVA 新手。 我試圖編寫一個應用程序,接下來將執行 - 打開到 UNIX 服務器的 ssh 連接,並使用私鑰從該服務器連接到另一台服務器。

要連接到第一台服務器,我正在使用如下程序:

public static void main(String args[])
{
String user = "john";
String password = "mypassword";
String host = "192.168.100.23";
int port=22;

try
    {
    JSch jsch = new JSch();
    Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
    System.out.println("Establishing Connection...");
    session.connect();
        System.out.println("Connection established.");
    System.out.println("Crating SFTP Channel.");
    ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
    sftpChannel.connect();
    System.out.println("SFTP Channel created.");


    InputStream out= null;
    out= sftpChannel.get(remoteFile);
    BufferedReader br = new BufferedReader(new InputStreamReader(out));
    String line;
    while ((line = br.readLine()) != null)
        System.out.println(line);
    br.close();
    }
catch(Exception e){System.err.print(e);}
}

它工作正常。 但是當我嘗試使用相同的代碼連接到第二個時,這不起作用,因為應用程序試圖創建一個全新的連接而不使用已經創建的連接。

有人知道如何讓應用程序使用已創建的連接嗎?

謝謝。

您可以在第一個session中使用端口轉發來做到這一點,然后在轉發的本地端口中創建另一個連接到主機的session 示例JumpHosts.java正是這樣做的。 公鑰認證參考UserAuthPubKey.java

這是基於這些示例對您的代碼的修改:

String user = "john";
String password = "mypassword";
String host = "127.0.0.1";
String host2 = "192.168.100.23";
int port=22;

try{
    JSch jsch = new JSch();
    Session session1 = jsch.getSession(user, host, port);
    session1.setPassword(password);
    session1.setConfig("StrictHostKeyChecking", "no");
    System.out.println("Establishing Connection...");
    session1.connect();
    System.out.println("Connection established.");

    //Here we do port forwarding to the second host
    int assinged_port = session1.setPortForwardingL(0, host2, 22);
    System.out.println("portforwarding: "+
            "localhost:"+assinged_port+" -> "+host+":"+22);

    //And here we connect to the first host to the forwarded port, not 22
    Session session2 = jsch.getSession(user, host, assinged_port);

    //This is your public key file 
    jsch.addIdentity("~/.ssh/id_rsa");
    session2.setConfig("StrictHostKeyChecking", "no");
    session2.connect();
    System.out.println("The session has been established to "+
            user+"@"+host);

    System.out.println("Crating SFTP Channel.");
    ChannelSftp sftpChannel = (ChannelSftp) session2.openChannel("sftp");
    sftpChannel.connect();
    System.out.println("SFTP Channel created.");


    InputStream out= null;
    out= sftpChannel.get(remoteFile);
    BufferedReader br = new BufferedReader(new InputStreamReader(out));
    String line;
    while ((line = br.readLine()) != null)
        System.out.println(line);
    br.close();
}
catch(Exception e){System.err.print(e);}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM