/** * Create a new sftp channel to execute commands. * * @param command to execute on the remote machine * @return a new execution channel * @throws SftpException if a channel could not be opened */ public ChannelExec getExecChannel(String command) throws SftpException { ChannelExec channelExec; try { channelExec = (ChannelExec) this.session.openChannel("exec"); channelExec.setCommand(command); channelExec.connect(); return channelExec; } catch (JSchException e) { throw new SftpException(0, "Cannot open a channel to SFTP server", e); } }
/** * * {@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); } }
InputStream in = null; try { channel.connect();// 执行命令 等待执行结束 in = channel.getInputStream(); return IoUtil.read(in, CharsetUtil.CHARSET_UTF_8);
InputStream in = null; try { channel.connect();// 执行命令 等待执行结束 in = channel.getInputStream(); return IoUtil.read(in, CharsetUtil.CHARSET_UTF_8);
void connect() throws JSchException { channel.connect(); }
void connect() throws JSchException { channel.connect(); }
channel.setCommand(commandName); setupStreams(); channel.connect(timeout > 0 ? timeout * 1000 : 0); if (!channel.isConnected()) { closeOutputStream();
ChannelExec chan = mySession.openChannel("exec"); chan.setCommand("du ... | awk ..."); // Set input and output streams // Request a PTY if you think you need one chan.connect();
@Override public void connect(final SocketFactory socketFactory, final String targetHost, final int targetPort, final int timeout) throws Exception { session = SftpClientFactory.createConnection(proxyHost, proxyPort, proxyUser.toCharArray(), proxyPassword.toCharArray(), proxyOptions); channel = (ChannelExec) session.openChannel("exec"); channel.setCommand(String.format(commandFormat, targetHost, targetPort)); channel.connect(timeout); }
@Override public void connect(final SocketFactory socketFactory, final String targetHost, final int targetPort, final int timeout) throws Exception { session = SftpClientFactory.createConnection(proxyHost, proxyPort, proxyUser.toCharArray(), proxyPassword.toCharArray(), proxyOptions); channel = (ChannelExec) session.openChannel("exec"); channel.setCommand(String.format(commandFormat, targetHost, targetPort)); channel.connect(timeout); }
@Override public void connect(final SocketFactory socketFactory, final String targetHost, final int targetPort, final int timeout) throws Exception { session = SftpClientFactory.createConnection(proxyHost, proxyPort, proxyUser.toCharArray(), proxyPassword.toCharArray(), proxyOptions); channel = (ChannelExec) session.openChannel("exec"); channel.setCommand(String.format(commandFormat, targetHost, targetPort)); channel.connect(timeout); }
@Override public void connect(final SocketFactory socketFactory, final String targetHost, final int targetPort, final int timeout) throws Exception { session = SftpClientFactory.createConnection(proxyHost, proxyPort, proxyUser.toCharArray(), proxyPassword.toCharArray(), proxyOptions); channel = (ChannelExec) session.openChannel("exec"); channel.setCommand(String.format(commandFormat, targetHost, targetPort)); channel.connect(timeout); }
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();
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();
public void copyFile(Session session, String sourceFile, String destinationFile) { ChannelExec channel = (ChannelExec) session.openChannel("exec"); channel.setCommand("cp " + sourceFile + " " + destinationFile); channel.connect(); while(channel.isConnected()) { Thread.sleep(20); } int status = channel.getExitStatus(); if(status != 0) throw new CopyException("copy failed, exit status is " + status); }
@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; }