vedo
vedo

Reputation: 21

Java open new ssh connection from already existing

I am new in JAVA. I trying to write an application that will do next - open ssh connection to UNIX server and from this server connection to another server using private key.

To connect to first server I am using program like:

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);}
}

And it works fine. But when I try to use the same code to connect to second this doesn't work, as application tries to create a completely new connection without using already created one.

Does someone know how can I forse application to use already created connection?

thanks.

Upvotes: 2

Views: 962

Answers (1)

Omar Mainegra
Omar Mainegra

Reputation: 4184

You can do that using port forwarding in the first session and then creating another session that connect to the host in the local port forwarded. The example JumpHosts.java does exactly that. For public key authentication refers to UserAuthPubKey.java

This is a modification to your code based on those examples:

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);}

Upvotes: 2

Related Questions