Hi Folks,
I am trying to send out multiple commands using the same channel and grab the out output stating whether the execution was succssful or not?
But when I am trying to send out 2 commands in row, its not working.
PLease find code below:
package eclipsepackage; import java.io.IOException; import java.io.InputStream; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSchException; //import IOExceptions; public class GetLastCommandStatus extends VerifyHostReachable { public int rVal = 0; public String cmd = "ls"; public String cmd1 = "echo $?"; public void cHKval() throws JSchException, IOException { ChannelExec channel3 = (ChannelExec) session.openChannel("exec"); channel3.setCommand(cmd ; cmd1); //cmd1 will only be run if the first command is successful channel3.setInputStream(null); InputStream in = channel3.getInputStream(); channel3.connect(); gLastCmdStatus(in,channel3); channel3.disconnect(); //channel3.setInputStream(); } private void gLastCmdStatus(InputStream in, ChannelExec channel3) throws IOException { //byte[] b = new byte[10]; while (true) { while (in.available() > 0) { System.out.println("Execution of last command was not successful;\n"); byte[] tmp = new byte[1024]; int i = in.read(tmp, 0, 1024); /* Number of bytes actually read is returned as an integer. */ if (i < 0) { break; } String line = new String(tmp, 0, i); System.out.println(line); } if (channel3.isClosed()) { break; } try { Thread.sleep(1000); } catch (Exception ee) { //ignore } } } }
Its giving error when I am passing 2 commands in row...
cmd1 will let me know the result of first cmd executed.
Please let me know. Thanks in advance !!!
Thanks,
Asif Masood