I am very new to JSch. I am trying to run few commands on a server which I am getting as input from some other system. What I am doing is taking those commands and passing them as a parameter to a java method. For Eg:
public String readFileFromPath(String server, String path,
String fileName);
Here first we have to cd to 'path', then we need to read some particular content from the file present on the path. To implement this I did following :
Session session = sshOperations.getSessionWithTimeout(USER,server,SSHPORT,1000);
Channel shellChannel = sshOperations.getShellChannel(session);
InputStream in = new PipedInputStream();
PipedOutputStream consoleInput = new PipedOutputStream((PipedInputStream) in);
OutputStream out = new PipedOutputStream();
BufferedReader consoleOutput = new BufferedReader(new InputStreamReader(new PipedInputStream((PipedOutputStream) out)));
shellChannel.setInputStream(in);
shellChannel.setOutputStream(out);
shellChannel.connect(1000);
consoleInput.write(("cd "+path).getBytes());
// first While
while ((line = consoleOutput.readLine()) != null)
{
System.out.println("check "+ line);
}
// execute second command
consoleInput.write("cat some.properties".getBytes());
// second While
while ((line = consoleOutput.readLine()) != null)
{
System.out.println("check "+ line);
}
Now what I know is whenever I connect to that server I get a welcome text :
"You are using <serverName> server.
Please contact admin for any issues"
So, after the first while loop my cd command ran and it prints the message mentioned above. But, after this it waits for more output from the output stream (it is stuck at this point )and the output stream can't product anything until I run another command.
Somehow I want to exit from the first while loop without writing the logic for consuming the 2 lines(fixed lines). As for the next command I will not know how many lines will come as output in stream.
Please suggest the logic to the get the desired output i.e. I ran a command and some logic consumes it and then I get get to run another command and so on until all the commands which came as parameter are executed.
Is there any other way to achive the same?
Thanks