java - Sending commands to server via JSch shell channel -


i can't figure out how can send commands via jsch shell channel.

i this, doesn't work:

jsch shell = new jsch(); string command = "cd home/s/src";   session session = shell.getsession(username, host, port);   myuserinfo ui = new myuserinfo();   ui.setpassword(password);   session.setuserinfo(ui);   session.connect();    channel = session.openchannel("shell");   fromserver = new bufferedreader(new inputstreamreader(channel.getinputstream()));   toserver = channel.getoutputstream(); channel.connect();   toserver.write((command + "\r\n").getbytes()); toserver.flush(); 

and read input this:

stringbuilder builder = new stringbuilder();    int count = 0;   string line = "";    while(line != null) {       line = fromserver.readline();     builder.append(line).append("\n");      if (line.endswith(".") || line.endswith(">")){         break;     } }   string result = builder.tostring();   consoleout.println(result); 

if hangs @ readline() means either "while" never ending (might unlikely considering code), or, readline() waiting source, namely iostream blocks thread cause available()!=true.

i can't quite troubleshoot code without seeing debug info. advice, have tried pipedintputstream? idea pipe console input "your" output can "write" it. implement this, need initialize in/out-put.

inputstream in = new pipedinputstream(); pipedoutputstream pin = new pipedoutputstream((pipedinputstream) in); /**...*/ channel.setinputstream(in); channel.connect(); /** ...*/ pin.write(myscript.getbytes()); 

the same goes question, how read console output.

pipedinputstream pout = new pipedinputstream((pipedoutputstream) out); /** * ... */ bufferedreader consoleoutput = new bufferedreader(new inputstreamreader(pout)); consoleoutput.readline(); 

and again, if not sure how many lines read , want use "while", make sure inside while prevent 1) busy-waiting 2) ending-condition. example:

while(!end) {    consoleoutput.mark(32);    if (consoleoutput.read()==0x03) end = true;//end of text    else    {       consoleoutput.reset();      consoleoutput.readline();      end = false;    } } 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -