2

I am looking for a solution to set of commands that need to be executed one after another in a sequential order. Again one command should execute only after the previous command is completed its execution.

String command="cd /home/; ls-ltr;"
java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();
            Session session=jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");

            Channel channel=session.openChannel("exec");
            ((ChannelExec)channel).setCommand(command);
            channel.setInputStream(null);
            ((ChannelExec)channel).setErrStream(System.err);

            InputStream in=channel.getInputStream();
            channel.connect();
            byte[] tmp=new byte[1024];
            while(true){
              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()){
                System.out.println("exit-status: "+channel.getExitStatus());
                break;
              }
              try{Thread.sleep(1000);}catch(Exception ee){}
            }
              if(channel.isClosed()){
                System.out.println("exit-status: "+channel.getExitStatus());
                break;
              }
              try{Thread.sleep(1000);}catch(Exception ee){}
            }

            System.out.println("DONE");

I tried executing command using ";" for each command but all the command are executed in a single attempt. So, its not working. What can be the possible approach to run each command in the same shell / exec.

7
  • Cerating a shell script on the target system and invoking that? Commented Dec 15, 2015 at 20:06
  • Yeah, i am just trying using shell. But i am not sure how to execute one command after execution of the previous command. Commented Dec 15, 2015 at 20:11
  • ls -ltr /home/ would be just one command... what's the script supposed to do? Commented Dec 15, 2015 at 20:19
  • can you describe what your script should do in the end? Commented Dec 16, 2015 at 10:50
  • I changed the implementation from "exec" to shell and update the command with "&&" between each command. Now the following command is executed only after the complete execution of the previous one. That is "&&" works in such a way that, based on the success status of the previous command i.e by checking the exit status which should be "0" else the following command wont execute. Commented Dec 17, 2015 at 17:56

2 Answers 2

1

I changed the implementation from "exec" to "shell" and update the command with "&&" between each command. Now the following command is executed only after the complete execution of the previous one. That is "&&" works in such a way that, based on the success status of the previous command i.e by checking the exit status which should be "0" else the following command wont be executed.

Updated code:

try{    
            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();
            Session session=jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");

            Channel channel=session.openChannel("shell");
            OutputStream ops = channel.getOutputStream();
            PrintStream ps = new PrintStream(ops, true);

             channel.connect();
             ps.println(command1 + "&&" + command2 + "&&" + command3 +"&&" +command4);
            InputStream in=channel.getInputStream();
            byte[] tmp=new byte[1024];
            while(true){
              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()){
                System.out.println("exit-status: "+channel.getExitStatus());
                break;
              }
              try{Thread.sleep(1000);}catch(Exception ee){}
            }
            channel.disconnect();
            session.disconnect();
            System.out.println("DONE");
        }catch(Exception e){
            e.printStackTrace();
        }
Sign up to request clarification or add additional context in comments.

Comments

0

You can use one command after another by appending them with &&. For example,

jschUtil.openChannel("exec");
jschUtil.getExecSshChannel().setCommand("cd /root/Test1/Test2 && mkdir Neel");

Here, Directory Neel will be created inside Test2. If you create 2 separate channel or use the commands one after the other, this will never be possible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.