/** * * {@link org.apache.commons.vfs2.provider.sftp.SftpFileSystem#executeCommand(java.lang.String, java.lang.StringBuilder) } */ private int executeCommand( String command, StringBuilder output ) throws JSchException, IOException { this.ensureSession(); ChannelExec channel = (ChannelExec) this.session.openChannel( "exec" ); channel.setCommand( command ); channel.setInputStream( (InputStream) null ); InputStreamReader stream = new InputStreamReader( channel.getInputStream() ); channel.setErrStream( System.err, true ); channel.connect(); char[] buffer = new char[128]; int read; while ( ( read = stream.read( buffer, 0, buffer.length ) ) >= 0 ) { output.append( buffer, 0, read ); } stream.close(); while ( !channel.isClosed() ) { try { Thread.sleep( 100L ); } catch ( Exception exc ) { log.logMinimal( "Warning: Error session closing. " + exc.getMessage() ); } } channel.disconnect(); return channel.getExitStatus(); }
/** * Execute a command through the ssh session, pumping its * stderr and stdout to our own logs. */ private int execCommand(Session session, String cmd) throws JSchException, InterruptedException, IOException { LOG.debug("Running cmd: " + cmd); ChannelExec exec = null; try { exec = (ChannelExec)session.openChannel("exec"); exec.setCommand(cmd); exec.setInputStream(null); exec.connect(); // Pump stdout of the command to our WARN logs StreamPumper outPumper = new StreamPumper(LOG, cmd + " via ssh", exec.getInputStream(), StreamPumper.StreamType.STDOUT); outPumper.start(); // Pump stderr of the command to our WARN logs StreamPumper errPumper = new StreamPumper(LOG, cmd + " via ssh", exec.getErrStream(), StreamPumper.StreamType.STDERR); errPumper.start(); outPumper.join(); errPumper.join(); return exec.getExitStatus(); } finally { cleanup(exec); } }
channel.setInputStream(null); channel.setErrStream(errStream); InputStream in = null;
channel.setInputStream(null); channel.setErrStream(errStream); InputStream in = null;
channel.setInputStream(null, true); channel.setInputStream(null); channel.connect();
ChannelExec deployWarFile = (ChannelExec)session.openChannel("exec"); deployWarFile.setCommand("/path/to/count-the-bytes"); deployWarFile.setOutputStream(System.out); deployWarFile.setInputStream(new BufferedInputStream(new FileInputStream(warFile))); deployWarFile.connect();
private void checkForRosCore() throws JSchException, IOException { int pid = -1; ChannelExec channel = (ChannelExec) session.openChannel("exec"); channel.setCommand("ps aux | grep roscore | grep -v grep | awk '{ print $2 }'"); channel.setInputStream(null); channel.setOutputStream(System.out); BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInputStream())); channel.connect(CONNECTION_TIMEOUT); String tmp = reader.readLine(); if (tmp != null) pid = Integer.parseInt(tmp); channel.disconnect(); if (pid < 0) { shellPrintStream.println("roscore &"); } }
private ChannelExec createChannel(final PrintStream logger, final Session session) throws JSchException { final ChannelExec channel = (ChannelExec) session.openChannel("exec"); channel.setOutputStream(logger, true); channel.setExtOutputStream(logger, true); channel.setInputStream(null); if (pty == null) { pty = Boolean.FALSE; } channel.setPty(pty); return channel; }
public class sshConnect extends AsyncTask <String, Void, String>{ ByteArrayOutputStream Baos=new ByteArrayOutputStream(); ByteArrayInputStream Bais = new ByteArrayInputStream(new byte[1000]); @Override protected String doInBackground(String... data) { cSession jschses = new cSession(); Session ses =null; ses = jschses.Met1(); try { ses.connect(); ChannelExec channel = (ChannelExec)ses.openChannel("exec"); channel.setOutputStream(Baos); channel.setInputStream(Bais); //Run Command channel.setCommand("python ~/BBB_test/testconnect.py"); channel.connect(); try{Thread.sleep(3500);}catch (Exception ee){} channel.disconnect(); //session.disconnect(); }catch (Exception e){ System.out.println(e.getMessage()); } return Baos.toString(); }
channel.setInputStream(null); channel.setOutputStream(System.out);
@Override public int exec() throws IOException { try { final ChannelExec channel = ChannelExec.class.cast( this.session.openChannel("exec") ); channel.setErrStream(this.stderr, false); channel.setOutputStream(this.stdout, false); channel.setInputStream(this.stdin, false); channel.setCommand(this.command); channel.connect(); Logger.info(this, "$ %s", this.command); return this.exec(channel); } catch (final JSchException ex) { throw new IOException(ex); } finally { this.session.disconnect(); } }
public void sendCommandThroughExecChannel(LocalCloudMachines machine, String command) { try { ChannelExec channel = (ChannelExec) sessionForMachine(machine).openChannel("exec"); channel.setCommand(command); channel.setInputStream(null); channel.setOutputStream(System.out); } catch (JSchException e) { e.printStackTrace(); } }
protected boolean send(String cmd) { logger.debug("sending command: {}", cmd); ChannelExec channel = null; boolean success = true; try { channel = (ChannelExec) session.openChannel("exec"); channel.setCommand(cmd); channel.setInputStream(null); InputStream stdout = channel.getInputStream(); InputStream stderr = channel.getErrStream(); channel.connect(TIMEOUT); success = waitForChannelClosed(channel, stdout, stderr); } catch (IOException | JSchException e) { logger.error(e.getMessage().replace("\n", " ")); success = false; } finally { if (channel != null) { channel.disconnect(); } } return success; }
@Override public int exec() throws IOException { try { final ChannelExec channel = ChannelExec.class.cast( this.session.openChannel("exec") ); channel.setErrStream(this.stderr, false); channel.setOutputStream(this.stdout, false); channel.setInputStream(this.stdin, false); channel.setCommand(this.command); channel.connect(); Logger.info(this, "$ %s", this.command); return this.exec(channel); } catch (final JSchException ex) { throw new IOException(ex); } finally { this.session.disconnect(); } }
//Start EXEC ChannelExec ce = (ChannelExec) session.openChannel("exec"); ce.setCommand("screen -S " + name + " -X stuff 'quit\n'"); ce.setInputStream(null); ce.setErrStream(System.err); InputStream in=ce.getInputStream(); ce.connect(); byte[] tmp=new byte[1024]; while(true){ while(in.available()>0){ int i=in.read(tmp, 0, 1024); if(i<0)break; //System.out.print(new String(tmp, 0, i)); } if(ce.isClosed()){ if(in.available()>0) continue; //System.out.println("exit-status: "+ce.getExitStatus()); break; } try{Thread.sleep(1000);}catch(Exception ee){} } ce.disconnect(); //STOP EXEC
public ChannelExecWrapper( Session session, String command, InputStream stdIn, OutputStream stdOut, OutputStream stdErr ) throws JSchException, IOException { this.command = command; this.channel = (ChannelExec) session.openChannel( "exec" ); if ( stdIn != null ) { this.passedInStdIn = stdIn; this.channel.setInputStream( stdIn ); } if ( stdOut != null ) { this.passedInStdOut = stdOut; this.channel.setOutputStream( stdOut ); } if ( stdErr != null ) { this.passedInStdErr = stdErr; this.channel.setErrStream( stdErr ); } this.channel.setCommand( command ); this.channel.connect(); }
public ChannelExecWrapper( Session session, String command, InputStream stdIn, OutputStream stdOut, OutputStream stdErr ) throws JSchException, IOException { this.command = command; this.channel = (ChannelExec) session.openChannel( "exec" ); if ( stdIn != null ) { this.passedInStdIn = stdIn; this.channel.setInputStream( stdIn ); } if ( stdOut != null ) { this.passedInStdOut = stdOut; this.channel.setOutputStream( stdOut ); } if ( stdErr != null ) { this.passedInStdErr = stdErr; this.channel.setErrStream( stdErr ); } this.channel.setCommand( command ); this.channel.connect(); }
public void updateInterval(Session s, String filename) { String checkfortimeupdate = "fgrep '-send' \"public_html/final/" + filename + "\""; ChannelExec channel = (ChannelExec)session.openChannel("exec"); channel.setCommand(checkfortimeupdate); channel.setInputStream(null); channel.setErrStream(System.err); BufferedReader in = new BufferedReader(new InputStreamReader(channel.getInputStream(), encoding)); channel.connect(); eatOutput(in); if (channel.isClosed()) { System.out.println("UpdateInterval Closed exit-status: " + channel.getExitStatus()); break; } channel.disconnect(); }
public static Process start(SshRoot root, boolean tty, OutputStream out, String ... command) throws JSchException { TimedOutputStream dest; ChannelExec channel; dest = new TimedOutputStream(out); channel = root.createChannelExec(); // tty=true propagates ctrl-c to the remote host: // (unfortunately, this causes ssh servers to send cr/lf, and I didn't find // a way to stop this - I tried setPtyType and setTerminalMode) channel.setPty(tty); // TODO: http://tools.ietf.org/html/rfc4250 // TODO: channel.setTerminalMode(new byte[] { 70, 0, 0, 0, 0, /*71, 0, 0, 0, 0,*/ 0 }); channel.setCommand(Separator.SPACE.join(command)); channel.setInputStream(null); channel.setOutputStream(dest); channel.setExtOutputStream(dest); channel.connect(); return new Process(root, command, channel, dest); }
public static Process start(SshRoot root, boolean tty, OutputStream out, String ... command) throws JSchException { TimedOutputStream dest; ChannelExec channel; dest = new TimedOutputStream(out); channel = root.createChannelExec(); // tty=true propagates ctrl-c to the remote host: // (unfortunately, this causes ssh servers to send cr/lf, and I didn't find // a way to stop this - I tried setPtyType and setTerminalMode) channel.setPty(tty); // TODO: http://tools.ietf.org/html/rfc4250 // TODO: channel.setTerminalMode(new byte[] { 70, 0, 0, 0, 0, /*71, 0, 0, 0, 0,*/ 0 }); channel.setCommand(Separator.SPACE.join(command)); channel.setInputStream(null); channel.setOutputStream(dest); channel.setExtOutputStream(dest); channel.connect(); return new Process(root, command, channel, dest); }