I'am tring to do a double hop SSH in java with jsch. it's something like this : scheme
I face a connection reset issue when I connect to the second session.
Here is the code :
String user=user; String host=host; String pswd=pass;
JSch jsch = new JSch();
Session session= jsch.getSession(user, host, 22);
session.setPassword(pswd);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
System.out.println("Connecting to host1 ");
session.connect();
lport=2248;
rhost= rhost;
rport= 22;
int assinged_port=session.setPortForwardingL(lport, rhost, rport);
String user2=user;
String pswd2= pswd;
Session session2=jsch.getSession(user2, "localhost", assinged_port);
session2.setPassword(pswd2);
session2.setConfig(config);
System.out.println("Connecting to rhost via localhost:" + assinged_port);
session2.connect();
Channel channel = session2.openChannel("exec");
((ChannelExec)channel).setCommand("sh run");
channel.setInputStream(null);
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
((ChannelExec)channel).setErrStream(errorStream);
ByteArrayOutputStream out = new ByteArrayOutputStream();
channel.setOutputStream(out);
channel.connect();
while(true){
if(channel.isClosed()){
System.out.println("exit-status: "+ channel.getExitStatus());
break;
}
}
String result =new String(out.toByteArray());
channel.disconnect();
Here is the resulting error :
com.jcraft.jsch.JSchException: Session.connect: java.net.SocketException: Connection reset at com.jcraft.jsch.Session.connect(Session.java:565) at com.jcraft.jsch.Session.connect(Session.java:183)
I don't know what I do wrong and how to join the second server.
Also tried the solution of this question -> SSH tunneling via JSch
but I got the same error "connection reset" on the second connection
Thanks in advance for your help and advises !