-1

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
  • 1
  • 3
  • _"But it's not successful"_ -- You have to tell us how it's not successful, and include any error message and/or stack trace (format a stack trace as code, not blockquote). – Jim Garrison Nov 30 '17 at 07:33
  • Ran a file creation command before &after the sshpass request to verify. file is created in Machine1, but did not work in Machine2. – Nandu Nov 30 '17 at 08:15

1 Answers1

0
ChannelExec(session_r, "ls -la >>result.txt")
ChannelExec(session_r, Cl_samp);
ChannelExec(session_r, "ls -la >>result.txt")

Each channel that you create will run independently of the others. In the common case (making an ssh connection to a unix server), each channel will invoke a separate shell instance, which runs the command that you specify and then exits. Running ssh in one channel won't affect subsequent commands launched in different channels.

To invoke ssh on the second host and get it to run a command on the third host, you need to do the whole thing through one exec channel, something like this:

String Cl_samp= "sshpass -p '...' ssh -o ... root@... 'ls -la >>result.txt'";
ChannelExec(session_r, Cl_samp);

This adds the command to be invoked to the ssh command line, which causes ssh (running on the second host) to request the command to be run on the third host.

Kenster
  • 23,465
  • 21
  • 80
  • 106
  • Thanks for the response. I tried as you mentioned above. But still no luck. If I run the command: sshpass -p '...' ssh -o ... root@... 'ls -la >>result.txt' directly from putty session of machine2, it works, file got created in machine3. The issue is when launching the same from the Java code. – Nandu Dec 01 '17 at 06:57