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.

user4130072
  • 137
  • 4
  • 17
  • Cerating a shell script on the target system and invoking that? – Jan Dec 15 '15 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. – user4130072 Dec 15 '15 at 20:11
  • `ls -ltr /home/` would be just one command... what's the script supposed to do? – Jan Dec 15 '15 at 20:19
  • can you describe what your script should do in the end? – Jan Dec 16 '15 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. – user4130072 Dec 17 '15 at 17:56
  • Changes made: Channel channel=session.openChannel("shell"); OutputStream ops = channel.getOutputStream(); PrintStream ps = new PrintStream(ops, true); channel.connect(); ps.println(command1 + "&&" + command2 + "&&" + command3); 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)); } – user4130072 Dec 17 '15 at 17:59
  • Possible duplicate of [How to perform multiple operations with JSch](https://stackoverflow.com/questions/7419513/how-to-perform-multiple-operations-with-jsch) – Gregor Jun 20 '17 at 14:05

2 Answers2

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();
        }
user4130072
  • 137
  • 4
  • 17
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.