Search code examples
javashellremote-server

channelExec.setCommand() Not Working when executing shell commands


I'm executing .sh file in a remote server and it is not working properly. For that i used JSch and java. and this is how i tried.

        ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
        InputStream in = channelExec.getInputStream();
        channelExec.setCommand("cd /home");

Solution

  • Here is a full example http://myitlearnings.com/java-code-to-run-a-remote-script-on-remote-host-using-ssh/ .

    Add this below statement, so the security measure is by passed // By default StrictHostKeyChecking is set to yes as a security measure.

    session.setConfig("StrictHostKeyChecking", "no");

    Add full code base (ip not required while sharing the code) and error message received. Add Proper exception handling, so we will get to know what is the error you are getting and can assist better