Re: [JSch-users] How do you capture output of a command executed on Shell?
Status: Alpha
Brought to you by:
ymnk
From: Sergey G. <Sergey.Grinev@Sun.COM> - 2008-09-15 23:37:58
|
You can just read from channel.getInputStream(). That's my code for running remote shell commands: try { channel = createChannel(); InputStream is = channel.getInputStream(); in = new BufferedReader(new InputStreamReader(is)); out = new StringWriter(); String line; while ((line = in.readLine()) != null || !channel.isClosed()) { if (line != null) { out.write(line + '\n'); out.flush(); } } in.close(); is.close(); setExitStatus(channel.getExitStatus()); } catch (JSchException jse) { log.warning("Jsch failure during running " + cmd); } catch (IOException ex) { log.warning("IO failure during running " + cmd); } finally { disconnect(); } Mike V wrote: > How do you capture output of a command executed on Shell, and how do > you know if it is done executing? > > I need this because some programs look for variables set by the > .profile (ksh). > > > This is how I'm doing it now: > -open Shell channel and send a ByteArrayInputStream that contains my > command, while outputing to a tmpfile.txt > -sleep for a bit and then (this is because i dont know when my command > is done running.. this is a retarded way of doing it since some > commands take a lot longer than others) > -open another channel(Exec) and send "cat tmpfile.txt" then read the > input stream > -remove the temp file > > > > ********************start of sample code********** > > commandToRun=commandToRun + " > "+tempFile+"\n"; > byte[] bytes = commandToRun.getBytes(); > ByteArrayInputStream bais=new ByteArrayInputStream(bytes); > > try { ..... //session connection stuff > ..... > > Channel channel=session.openChannel("shell"); > ((ChannelShell)channel).setInputStream(bais); > channel.connect(); > > try{Thread.sleep(1500);}catch(Exception ee){} > > Channel channel2=session.openChannel("exec"); > ((ChannelExec)channel2).setCommand("cat "+tempFile+"\n"); > InputStream in1=channel2.getInputStream(); > channel2.connect(); > > //channel 3 deletes the temp file > > **********************end of sample code ************************** > > > > There must be a better way of doing this... > What about a way to know when the command is done? Since the first > channel is a shell, i dont think you can wait for is to be .isClosed() > since it never is. > > Thank you very much for your help. > > > Mike > > > ------------------------------------------------------------------------ > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's challenge > Build the coolest Linux based applications with Moblin SDK & win great prizes > Grand prize is a trip for two to an Open Source event anywhere in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > > ------------------------------------------------------------------------ > > _______________________________________________ > JSch-users mailing list > JSc...@li... > https://lists.sourceforge.net/lists/listinfo/jsch-users > |