I am using JSch to run some commands after multi-level ssh:
public static void main(String[] args) {
String user="User0";
String ip="IP0";
int port=22;
String password="Password0";
JSch jsch= new JSch();
Session session=null;
ChannelExec channel=null;
try {
session=(jsch.getSession(user, ip, port));
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
String dir=Reomte_DIR;
String cmd1=SomeComplexCommand;
String cmd2=SomeMoreComplexCommand;
channel = (ChannelExec) session.openChannel("exec");
channel.setInputStream(null);
channel.setCommand("ssh User1@IP1_PasswordLessLogin;ssh User2@IP2_PasswordLessLogin; "+cmd1+" ; "+cmd2+" ;");
channel.setPty(true);
channel.connect(4000);
String res = null;
BufferedReader input = new BufferedReader(new InputStreamReader(channel.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(channel.getErrStream()));
if ((res = error.readLine()) == null) {
res = input.readLine()+input.readLine()+input.readLine()+input.readLine();
} else {
res = "-1";
}
System.out.println("result:"+res);
} catch (JSchException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
channel.disconnect();
session.disconnect();
}
}
but it doesn't give desired result.
Infact channel.getInputStream()
hangs. If I remove multilevel ssh, everything works fine!
Am i doing something wrong??
I got some hint from: Multi-level SSH login in Java and Multiple commands using JSch but i am not able to get my code running.