user4130072
user4130072

Reputation: 137

Executing multiple commands one after another using java JSch library

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.

Upvotes: 2

Views: 9203

Answers (2)

baidehi ghosh
baidehi ghosh

Reputation: 41

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.

Upvotes: 0

user4130072
user4130072

Reputation: 137

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();
        }

Upvotes: 1

Related Questions