This is the sequence I want to achieve :
Connect to Bastion Server -> Connect to web server -> Connect to MySQL
I am successfully connected to the Bastion but no luck on web server which can be only accessible after Bastion server.
I have different host, user and ppk file respective to Bastion and Web Server.
Here is the code I have tried where Bastion server seems connecting successfully but then on next server I am getting "connection Timeout"
public void jumpServer() {
try {
JSch jsch = new JSch();
jsch.addIdentity("c:\\user\\file1.ppk");
String host_1 = "x.xx.xx.x";
String user_1 = "myec-user";
Session session = jsch.getSession(user_1, host_1, 22);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection......");
session.connect();
//successfully connected to bastion and then tried to connect next server which is behind the bastion
jsch.addIdentity("c:\\user\\file2.ppk");
String host_2 = "xxx.xx.xx.xx";
String user_2 = "webuser";
int assinged_port = session.setPortForwardingL(0, host_2, 22);
Session session2 =jsch.getSession(user_2, host_2, assinged_port);
session2.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection......");
session2.connect();
System.out.println("Web Connected.");
} catch (JSchException ex) {
throw new RuntimeException(ex);
}
}
Error I am getting :
java.lang.RuntimeException: com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect
Note : Everything works fine via putty , only issue is when trying to connect using above code.