You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by wael mashal <wm...@gmail.com> on 2015/07/05 10:25:19 UTC
Return command result one by one as string
Hi
I write a code using sshd to execute more than one commands at the same
time by using PipedOutputStream , what I need to get each command result as
string to view it in mu jsp page in specific field , what i have now all
the data flushed together , please see the below code and help me to get
command result one by one ,
* final ClientChannel channel = session.createShellChannel();*
* ByteArrayOutputStream sent = new ByteArrayOutputStream();*
* PipedOutputStream pipedIn = new TeePipedOutputStream(sent);*
* channel.setIn(new PipedInputStream(pipedIn));*
* ByteArrayOutputStream out = new ByteArrayOutputStream();*
* ByteArrayOutputStream err = new ByteArrayOutputStream();*
* channel.setOut(out);*
* channel.setErr(err);*
* channel.open();*
* pipedIn.write("free -m\n".getBytes());*
* pipedIn.flush();*
* StringBuilder sb = new StringBuilder();*
* sb.append("mpstat");*
* sb.append("\n");*
* pipedIn.write(sb.toString().getBytes());*
* pipedIn.write("exit\n".getBytes());*
* pipedIn.flush();*
* channel.waitFor(ClientChannel.CLOSED, 0);*
*System.out.println(out.toString());*
*==========================================*
* class TeePipedOutputStream extends PipedOutputStream { private
OutputStream tee; public TeePipedOutputStream(OutputStream tee) {
this.tee = tee; } @Override public void write(int b) throws
IOException { super.write(b); tee.write(b); }
@Override public void write(byte[] b, int off, int len) throws
IOException { super.write(b, off, len); tee.write(b, off,
len); }}Thanks*