java - How to catch remote input using JSch ChannelExec -


i wrote program runs command on remote machine , halts. there program:

import com.jcraft.jsch.*; import java.io.*;  public class jschtest {     private static string readstring(string prompt) {              if (prompt != null) {             system.out.println(prompt);         }         bufferedreader in = new bufferedreader(new inputstreamreader(system.in));           string input = null;         try {             input = in.readline();         } catch (ioexception e) {             system.err.println(e);         }                 return input;     }      private static boolean readboolean(string prompt) {         while (true) {             string input = readstring(prompt);             if (input.equalsignorecase("y") || input.equalsignorecase("n")) {                 return input.equalsignorecase("y");             } else {                 system.out.println("enter y or n.");             }         }     }      public static void main(string[] args) throws exception {           jsch jsch = new jsch();         session session = jsch.getsession(readstring("login:"),             readstring("server:"), integer.parseint(readstring("port:")));         session.setuserinfo(             new userinfo() {                 @override                 public string getpassphrase() {                     return readstring("passphrase:");                 }                  @override                 public string getpassword() {                     return readstring("password:");                 }                  @override                 public boolean promptpassword(string message) {                     return readboolean(message);                 }                  @override                 public boolean promptpassphrase(string message) {                     return readboolean(message);                 }                  @override                 public boolean promptyesno(string message) {                     return readboolean(message);                 }                  @override                 public void showmessage(string message) {                     system.out.println(message);                 }             }         );         session.connect();         channelexec channel = (channelexec)session.openchannel("exec");         inputstream in = channel.getinputstream();         channel.setcommand(readstring("command:"));         channel.connect();                  byte[] buffer = new byte[1024];          int bytes;         {             while (in.available() > 0) {                 bytes = in.read(buffer, 0, 1024);                 system.out.print(new string(buffer, 0, bytes));             }         } while (!channel.isclosed());                                 channel.disconnect();         session.disconnect();                 }     } 

this program works fine when i'm using commands produce output only, such echo hello. when i'm trying pass in command such read var;echo entered: $var program running infinite loop, because channel isn't closed , waiting input.

ok, got channel's output stream write input for

        outputstream out = channel.getoutputstream(); 

and made i/o loop looking this:

        while (true) {             while (in.available() > 0) {                 bytes = in.read(buffer, 0, 1024);                 system.out.print(new string(buffer, 0, bytes));             }             if (channel.isclosed()) {                 break;             } else if (true /* channel.iswaitingforinput() */) {                 string output = readstring(null) + "\n";                 out.write(output.getbytes());                 out.flush();             }         } 

but can see - have no information happening on channel's other side. there input must provide now, or not? program asking input anytime, though not needed.

so there question - how can know when must pass in input channel, or, maybe, how can rewrite program won't running commands, providing input them, when needed (but halting @ end anyway)?

the channel stays open until disconnected. not disconnect automatically when send exit command , logged off host.

you need check channel.getexitstatus > -1. exit status -1 until session closed host (usually triggered user typing 'exit').

once exit status has returned other -1, can optionally process exit status (ie, clean exit, or there error). need disconnect channel after that:

  channel channel=session.openchannel("shell");    channel.setinputstream(system.in);   channel.setoutputstream(system.out);    channel.connect();    while (channel.getexitstatus() == -1){      try{thread.sleep(1000);}catch(exception e){system.out.println(e);}   }    channel.disconnect(); 

with loop above, standard input java console sent channel, , once user sends 'exit', channel disconnects.

there working example here: never ending of reading server response using jsch

also, jcraft example (which not close when 'exit', otherwise working example). http://www.jcraft.com/jsch/examples/userauthki.java


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -