7 Replies Latest reply: Dec 3, 2010 9:11 AM by sabre150 RSS

    getRuntime.exec with SSH

    542405
      Hi,

      I need to run a ssh command using the Runtime. The problem is that I cannot use the Jsch or any library since the host is not a normal/interactive ssh server but rather requires the command to be issued along the connection string and terminates the session automatically after command execution.

      If I run only String[] cmds = {"ssh.exe" } only or pass any invalid options, I get the ssh response. But when I run the command the command with all valid parameters (as below), it hangs and does not generate any error or input stream.

      I tried String[] cmds = {"cmd","/c" ,"ssh.exe"....... } as well but it hangs and does not return any output.

      Not sure what I am missing.


      String[] cmds = {"ssh.exe", "-i", "C:/id_dsa.txt", "ahmedcli@ipaddress", "GetPwdRequest", "3652"};
      Process p = Runtime.getRuntime().exec(cmds)

      StreamGobbler sg = new StreamGobbler(p.getErrorStream(), "ERROR");
      StreamGobbler sg1 = new StreamGobbler(p.getInputStream(), "InputStream");
      sg1.start();
      sg.start();

      Edited by: user539402 on Nov 21, 2010 4:07 AM
        • 1. Re: getRuntime.exec with SSH
          802316
          Have you tried jssh or similar library. A purpose built library may be easier to use/manage.
          • 2. Re: getRuntime.exec with SSH
            542405
            I cannot use Jsch as the session is terminated if the command is not part of the ssh command. It should be something like

            ssh -i key user@host command

            Cheers
            • 3. Re: getRuntime.exec with SSH
              sabre150
              user539402 wrote:
              I cannot use Jsch as the session is terminated if the command is not part of the ssh command. It should be something like

              ssh -i key user@host command
              I'm not convinced of this. That looks to be a standard syntax; it certainly matches the ssh client/server syntax for my Ubuntu Linux which I contact using Jsch without any problems at all. Which ssh server type are you connecting to?
              • 4. Re: getRuntime.exec with SSH
                542405
                Yes - SSHExec did the job

                channel = (ChannelExec) session.openChannel("exec");
                channel.setCommand("GetPwdRequest 3652");
                channel.connect(3000);

                But now having an issue with the Outputstream. I need the command output in a String.If I set channel.setOutputStream(System.out); and do not close the channel, I get the command result.

                But I do not get any output if:

                1. I set the outputstream to System.out and close the session or channel
                2. I set the output to a ByteArrayOutputStream which I then I convert to String. This case I do not get any output even if I do not close the session or channel.

                To me it seems, its closing too fast for the output (probably its running its own thread). Any ideas?
                • 5. Re: getRuntime.exec with SSH
                  sabre150
                  user539402 wrote:
                  1. I set the outputstream to System.out and close the session or channel
                  2. I set the output to a ByteArrayOutputStream which I then I convert to String. This case I do not get any output even if I do not close the session or channel.

                  To me it seems, its closing too fast for the output (probably its running its own thread). Any ideas?
                  Once System.out is closed nothing else written to it is printed. You need to do as I have done and create an OutputStream (mine is called UncloseableOutputStream) that chains all method calls to a wrapped stream except the close() method which it ignores.
                  channel.setOutputStream(new UncloseableOutputStream(System.out));
                  By using this you can continue writing to System.out after the channel has finished.
                  • 6. Re: getRuntime.exec with SSH
                    542405
                    This is how I resolved it...thanks to Ant task SSHExec....
                             thread =
                                        new Thread() {
                    
                                            public void run() {
                                                while (!channel.isEOF()) {
                                                    if (thread == null) {
                                                        return;
                                                    }
                                                    try {
                                                        sleep(500);
                                                    } catch (Exception e) {
                                                        // ignored
                                                    }
                                                }
                                            }
                                        };
                    
                                thread.start();
                                thread.join(0);
                    • 7. Re: getRuntime.exec with SSH
                      sabre150
                      user539402 wrote:
                      This is how I resolved it...thanks to Ant task SSHExec....
                      thread =
                      new Thread() {
                      
                      public void run() {
                      while (!channel.isEOF()) {
                      if (thread == null) {
                      return;
                      }
                      try {
                      sleep(500);
                      } catch (Exception e) {
                      // ignored
                      }
                      }
                      }
                      };
                      
                      thread.start();
                      thread.join(0);
                      Then I obviously do not understand the problem you were having since I don't see how that solves anything!