/** * * {@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(); }
@Override public FileStatus getFileStatus(Path path) throws IOException { ChannelSftp channelSftp = null; ChannelExec channelExec1 = null; ChannelExec channelExec2 = null; try { channelSftp = this.fsHelper.getSftpChannel(); SftpATTRS sftpAttrs = channelSftp.stat(HadoopUtils.toUriPath(path)); FsPermission permission = new FsPermission((short) sftpAttrs.getPermissions()); channelExec1 = this.fsHelper.getExecChannel("id " + sftpAttrs.getUId()); String userName = IOUtils.toString(channelExec1.getInputStream()); channelExec2 = this.fsHelper.getExecChannel("id " + sftpAttrs.getGId()); String groupName = IOUtils.toString(channelExec2.getInputStream()); FileStatus fs = new FileStatus(sftpAttrs.getSize(), sftpAttrs.isDir(), 1, 0l, sftpAttrs.getMTime(), sftpAttrs.getATime(), permission, StringUtils.trimToEmpty(userName), StringUtils.trimToEmpty(groupName), path); return fs; } catch (SftpException e) { throw new IOException(e); } finally { safeDisconnect(channelSftp); safeDisconnect(channelExec1); safeDisconnect(channelExec2); } }
try (BufferedReader reader = new BufferedReader(new InputStreamReader(ch.getInputStream(), encoding))) { String line;
/** * 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); } }
try { in = channel.getInputStream(); return IoUtil.read(in, CharsetUtil.CHARSET_UTF_8); } catch (IOException e) {
try { in = channel.getInputStream(); return IoUtil.read(in, CharsetUtil.CHARSET_UTF_8); } catch (IOException e) {
private void setupStreams() throws IOException { inputStream = channel.getInputStream(); // JSch won't let us interrupt writes when we use our InterruptTimer // to break out of a long-running write operation. To work around // that we spawn a background thread to shuttle data through a pipe, // as we can issue an interrupted write out of that. Its slower, so // we only use this route if there is a timeout. OutputStream out = channel.getOutputStream(); if (timeout <= 0) { outputStream = out; } else { IsolatedOutputStream i = new IsolatedOutputStream(out); outputStream = new BufferedOutputStream(i, 16 * 1024); } errStream = channel.getErrStream(); }
try { in = channel.getInputStream(); channel.setErrStream(outputStreamStdErr);
@Override public InputStream getInputStream() { try { return channel.getInputStream(); } catch (final IOException e) { throw new IllegalStateException("IOException getting the SSH proxy input stream", e); } }
@Override public InputStream getInputStream() { try { return channel.getInputStream(); } catch (final IOException e) { throw new IllegalStateException("IOException getting the SSH proxy input stream", e); } }
@Override public InputStream openStandardOutput() throws IOException { LOG.debug("Opening remote standard output: {}", command); return channel.getInputStream(); }
@Override public InputStream getInputStream() { try { return channel.getInputStream(); } catch (final IOException e) { throw new IllegalStateException("IOException getting the SSH proxy input stream", e); } }
ChannelExec deployWarFile = (ChannelExec)session.openChannel("exec"); deployWarFile.setCommand("/path/to/count-the-bytes"); OutputStream deployWarFileStdin = deployWarFile.getOutputStream(); InputStream deployWarFileStdout = new BufferedInputStream(deployWarFile.getInputStream()); InputStream warFileInputStream = new FileInputStream(warFile); deployWarFile.connect(); IOUtils.copy(warFileInputStream, deployWarFileStdin); deployWarFileStdin.close(); warFileInputStream.close(); IOUtils.copy(deployWarFileStdout, System.out); deployWarFileStdout.close();
JSchCliProcess(Session session, ChannelExec channel) throws IOException { super(channel.getInputStream(), channel.getErrStream() , channel.getOutputStream()); this.session = session; this.channel = channel; }
private void write(ChannelExec c, String name, InputStream data, ScpConfiguration cfg) throws IOException { OutputStream os = c.getOutputStream(); InputStream is = c.getInputStream(); try { writeFile(name, data, os, is, cfg); } finally { IOHelper.close(is, os); } }
@Override public ChannelExec create() throws Exception { checkConnected(); String channel = "exec"; executor = (ChannelExec) sessionConnection.getSession().openChannel(channel); executor.setPty(true); executor.setCommand(command); inputStream = executor.getInputStream(); errStream = executor.getErrStream(); executor.connect(); return executor; }
@Override public ChannelExec create() throws Exception { checkConnected(); String channel = "exec"; executor = (ChannelExec) sessionConnection.getSession().openChannel(channel); executor.setPty(true); executor.setCommand(command); inputStream = executor.getInputStream(); errStream = executor.getErrStream(); executor.connect(); return executor; }
/** * Close the streams belonging to the given Process. * * @param process * the <code>Process</code>. * @throws IOException */ public static void closeStreams(ChannelExec process) throws IOException { FileUtils.close(process.getInputStream()); FileUtils.close(process.getOutputStream()); FileUtils.close(process.getErrStream()); }
private void attachStreams(final ChannelExec channel) throws IOException { streamHandler.setProcessInputStream(channel.getOutputStream()); streamHandler.setProcessOutputStream(channel.getInputStream()); streamHandler.setProcessErrorStream(channel.getErrStream()); streamHandler.start(); }
public void openStreams(ChannelExec exec, String encoding) throws IOException { Assert.isFalse(open, "Already open"); Assert.noNulls(exec); this.inputFeeder = getInputFeeder(context, exec); this.outputPumper = new StreamPumper(exec.getInputStream(), encoding, context.getStdout()); this.errorPumper = new StreamPumper(exec.getErrStream(), encoding, context.getStderr()); this.open = true; }