Need to establish a remote ssh into machine2 from a machine1. Trying the code below using ssh.but not succssfull. Ran a file creation command before &after the sshpass request to verify. file is created in Machine1 , but not worked in Machine2. Pls help. Is there any other options
String Cl_samp= new String("sshpass -p "xxxx" ssh -o StrictHostKeyChecking=no root@XXX.XX.115.71");
try{
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session_r=jsch.getSession(user, XXX.XX.115.70, 22);
session_r.setConfig("StrictHostKeyChecking", "no");
session_r.setPassword(password);
session_r.setConfig(config);
session_r.connect();
ChannelExec(session_r, "ls -la >>result.txt")
ChannelExec(session_r, Cl_samp);
ChannelExec(session_r, "ls -la >>result.txt")
session_r.disconnect();
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}
private void ChannelExec(Session session_1, String Serv_cmd) throws IOException{
try {
Channel channel;
channel = session_1.openChannel("exec");
((ChannelExec)channel).setCommand(Serv_cmd);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
System.out.print("Server-"+"command:"+Serv_cmd+" \n");
channel.disconnect();
} catch (JSchException ex) {
Logger.getLogger(ContactEditorUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
– Nandu Nov 30 '17 at 08:15