0

I am in process to run shell script on remote server using Jsch where I managed to progress (Thanks for answered a question here) but my code still has couple of issue.

  1. It doesn't run commands in sequence and consistently given using printscreen.println() method.
  2. My program executes shell commands well in debug mode with some breakpoints but doesn't execute commands in run mode or without debug point(s).

commands in runScript method in ps.println() method which do not run in sequence consistently. also printResilt() method fails to print(execute) commands in run mode.

    public void runScript(Channel channel) throws Exception {
    // Gets an InputStream for this channel. All data arriving in as messages from the remote side can be read from this stream.
    try {

        OutputStream ops = channel.getOutputStream();
        PrintStream ps = new PrintStream(ops, true);

        channel.setOutputStream(System.out, true);

        // Execute the command
        channel.connect();

        InputStream in = channel.getInputStream();

        ps.println("sudo su - sudouser");
        ps.println("ksh -x \"/path/to/script/shellscript.sh\" arg1  arg2 arg3 arg4 arg5 arg6  arg7");
        ps.println("exit");
        ps.close();

        printResult(in, channel);
    }
    catch(IOException ioExp) {
        ioExp.printStackTrace();
    }


//retrieve the exit status of the remote command corresponding to this channel
 //int exitStatus = channelExec.getExitStatus();

    //Safely disconnect channel and disconnect session. If not done then it may cause resource leak
    channel.disconnect();
    session.disconnect();
}
 /**
    * @param input
    * @param channel
    */
   private static void printResult(InputStream in,
                                   Channel channel) throws Exception
   {
      int SIZE = 1024;
      byte[] tmp = new byte[SIZE];
      while (true) {
          try {
              Thread.sleep(1000);
          } catch (Exception ee) {}
          while (in.available() > 0) {
              int i = in.read(tmp, 0, 1024);
              if (i < 0)
                  break;
              System.out.print(new String(tmp, 0, i));
          }
          if (channel.isClosed()) {
               if(in.available() > 0) {
                  int i = in.read(tmp, 0, 1024);
                  System.out.print(new String(tmp, 0, i));
               } 
              System.out.println("exit-status: " + channel.getExitStatus());
              break;
          }

      }
   }

Main Method:

public class App 
{

public static void main( String[] args ) throws Exception
{   
    try
    {
        SFTPUploader sftpUploader = new SFTPUploader();
        ChannelSftp sftpChannel = sftpUploader.connectServer();

        //close sftp channel after upload
        sftpUploader.closeChannel(sftpChannel);

        //open executable channel
        Channel channel = sftpUploader.openChannel("shell");

        //execute shell script
        sftpUploader.runScript(channel);
    }
    catch(SftpException e) {
        e.printStackTrace();
    }
    catch(JSchException jschExp) {
        jschExp.printStackTrace();
    }
 }
}
MM Mathur
  • 1
  • 1
  • 1
    *"doesn't run commands in sequence and consistently"* - That's sooo vague! – Martin Prikryl Jan 28 '19 at 18:23
  • I meant to say that when it prints my commands, some time it prints first command in last or last command as second. Another issue that I face is commands are printed in part e.g. If I send command as: "ksh -x \"/path/to/script/shellscript.sh\" arg1 arg2 arg3 arg4 arg5 arg6 arg7" then some time it prints like: rg2 arg3 arg4. Which results like: -bash: rg2: command not found. Hope it clarifies my question – MM Mathur Jan 29 '19 at 06:20
  • Do you get the same problem even with simple commands like `echo`? – Martin Prikryl Jan 29 '19 at 06:24
  • @MartinPrikryl, with echo command prints without cut but doesn't run, as you can see the console output. `sudouser@serverhost ~$=>echo ksh -x "/path/to/scripts/scriptname.sh" arg1 arg2 arg3 arg4 arg5 arg6 arg7 ksh -x "/path/to/scripts/scriptname.sh" arg1 arg2 arg3 arg4 arg5 arg6 arg7 sudouser@servername ~$=> sudouser@servername ~$=>exit logout $ $ $ ` – MM Mathur Jan 29 '19 at 13:07
  • And if you use the correct way --- using "exec" channel, instead of "shell" channel? – Martin Prikryl Jan 29 '19 at 13:31
  • @MartinPrikryl, As you asked, I have tried using "exec" channel instead but it always runs first command only, even I disconnect existing and connect new channel before executing command. I also tried splitting multiple commands by semicolon but still it execute always first command only. – MM Mathur Jan 29 '19 at 17:57
  • Did you see [Executing sudo using SSH “exec” channel in JSch](https://stackoverflow.com/q/52481755/850848)? – Martin Prikryl Jan 29 '19 at 20:11
  • @MartinPrikryl I gave a try to all possible way in your provided link but nothing seems to be working. If I use multiple "exec" channel(closing and opening new channel after and before each command respectively), it still print execute only first command. – MM Mathur Jan 31 '19 at 18:28
  • Not sure why it forgive few characters always from command `ps.println("ksh -x script.sh arg1 arg2 arg3 arg4 arg5 arg6 arg7")` – MM Mathur Feb 04 '19 at 05:51
  • in above case it prints often something like: `username@hostname ~$=> arg1 arg2 arg3 arg4 arg5 arg6 arg7 -bash: arg1 : command not found` – MM Mathur Feb 04 '19 at 06:01
  • I observed that command goes wrong only if I execute `sudo su - username` before executing my next command as above comments. – MM Mathur Feb 04 '19 at 06:11

0 Answers0