Java Code Examples for com.jcraft.jsch.ChannelExec#setErrStream()
The following examples show how to use
com.jcraft.jsch.ChannelExec#setErrStream() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RemoteLauncherCommands.java From gemfirexd-oss with Apache License 2.0 | 8 votes |
/** * Connect to a remote host via SSH and execute a command. * * @param host * Host to connect to * @param user * User to login with * @param password * Password for the user * @param command * Command to execute * @return The result of the command execution */ private Result executeSshCommand(final String host, final String user, final String password, final String command) { StringBuilder result = new StringBuilder(); try { JSch jsch = new JSch(); Session session = jsch.getSession(user, host, 22); session.setUserInfo(createUserInfo(password)); session.connect(5000); ChannelExec channel = (ChannelExec) session.openChannel("exec"); channel.setCommand(command); channel.setInputStream(null); channel.setErrStream(System.err); InputStream in = channel.getInputStream(); channel.connect(); byte[] tmp = new byte[1024]; while (true) { while (in.available() > 0) { int i = in.read(tmp, 0, 1024); if (i < 0) break; result.append(new String(tmp, 0, i)); } if (channel.isClosed()) { break; } } channel.disconnect(); session.disconnect(); } catch (Exception jex) { return createResult(Result.Status.ERROR, jex.getMessage()); } return createResult(Result.Status.OK, result.toString()); }
Example 2
Source File: SshExecutor.java From vividus with Apache License 2.0 | 6 votes |
@Override protected SshOutput executeCommand(ServerConfiguration serverConfig, Commands commands, ChannelExec channel) throws JSchException, IOException { channel.setAgentForwarding(serverConfig.isAgentForwarding()); SshOutput executionOutput = new SshOutput(); try (ByteArrayOutputStream errorStream = new ByteArrayOutputStream()) { channel.setCommand(commands.getJoinedCommands()); channel.setErrStream(errorStream); channel.connect(); executionOutput.setOutputStream(readChannelInputStream(channel)); executionOutput.setErrorStream(new String(errorStream.toByteArray(), StandardCharsets.UTF_8)); executionOutput.setExitStatus(channel.getExitStatus()); } return executionOutput; }
Example 3
Source File: SSHHandlerTarget.java From embeddedlinux-jvmdebugger-intellij with Apache License 2.0 | 6 votes |
/** * Runs that java app with the specified command and then takes the console output from target to host machine * * @param path * @param cmd * @throws IOException */ private void runJavaApp(@NotNull final String path, @NotNull final String cmd) throws IOException, RuntimeConfigurationException { consoleView.print(NEW_LINE + EmbeddedLinuxJVMBundle.getString("pi.deployment.build") + NEW_LINE + NEW_LINE, ConsoleViewContentType.SYSTEM_OUTPUT); Session session = connect(ssh.get()); consoleView.setSession(session); try { ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); channelExec.setOutputStream(System.out, true); channelExec.setErrStream(System.err, true); List<String> commands = Arrays.asList( String.format("%s kill -9 $(ps -efww | grep \"%s\"| grep -v grep | tr -s \" \"| cut -d\" \" -f2)", params.isRunAsRoot() ? "sudo" : "", params.getMainclass()), String.format("cd %s", path), String.format("tar -xvf %s.tar", consoleView.getProject().getName()), "rm *.tar", cmd); for (String command : commands) { consoleView.print(EmbeddedLinuxJVMBundle.getString("pi.deployment.command") + command + NEW_LINE, ConsoleViewContentType.SYSTEM_OUTPUT); } channelExec.setCommand(LinuxCommand.builder().commands(commands).build().toString()); channelExec.connect(); checkOnProcess(channelExec); } catch (JSchException e) { setErrorOnUI(e.getMessage()); } }
Example 4
Source File: RemoteLauncherCommands.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Connect to a remote host via SSH and execute a command. * * @param host * Host to connect to * @param user * User to login with * @param password * Password for the user * @param command * Command to execute * @return The result of the command execution */ private Result executeSshCommand(final String host, final String user, final String password, final String command) { StringBuilder result = new StringBuilder(); try { JSch jsch = new JSch(); Session session = jsch.getSession(user, host, 22); session.setUserInfo(createUserInfo(password)); session.connect(5000); ChannelExec channel = (ChannelExec) session.openChannel("exec"); channel.setCommand(command); channel.setInputStream(null); channel.setErrStream(System.err); InputStream in = channel.getInputStream(); channel.connect(); byte[] tmp = new byte[1024]; while (true) { while (in.available() > 0) { int i = in.read(tmp, 0, 1024); if (i < 0) break; result.append(new String(tmp, 0, i)); } if (channel.isClosed()) { break; } } channel.disconnect(); session.disconnect(); } catch (Exception jex) { return createResult(Result.Status.ERROR, jex.getMessage()); } return createResult(Result.Status.OK, result.toString()); }
Example 5
Source File: SftpFileSystem.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * Executes a command and returns the (standard) output through a StringBuilder. * * @param command The command * @param output The output * @return The exit code of the command * @throws JSchException if a JSch error is detected. * @throws FileSystemException if a session cannot be created. * @throws IOException if an I/O error is detected. */ private int executeCommand(final String command, final StringBuilder output) throws JSchException, IOException { final ChannelExec channel = (ChannelExec) getSession().openChannel("exec"); try { channel.setCommand(command); channel.setInputStream(null); try (final InputStreamReader stream = new InputStreamReader(channel.getInputStream())) { channel.setErrStream(System.err, true); channel.connect(connectTimeoutMillis); // Read the stream final char[] buffer = new char[EXEC_BUFFER_SIZE]; int read; while ((read = stream.read(buffer, 0, buffer.length)) >= 0) { output.append(buffer, 0, read); } } // Wait until the command finishes (should not be long since we read the output stream) while (!channel.isClosed()) { try { Thread.sleep(SLEEP_MILLIS); } catch (final Exception ee) { // TODO: swallow exception, really? } } } finally { channel.disconnect(); } return channel.getExitStatus(); }
Example 6
Source File: SftpFileSystemWindows.java From pentaho-kettle with Apache License 2.0 | 5 votes |
/** * * {@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(); }
Example 7
Source File: Ssh.java From BigDataScript with Apache License 2.0 | 5 votes |
/** * Connect via ssh and execute a command (e.g. execute "ls -al" in remote host) * * Reference: http://www.jcraft.com/jsch/examples/Exec.java.html */ public String exec(String command) { // Open ssh session try { Gpr.debug("COMMAND: " + command); channel = connect("exec", command); ChannelExec chexec = (ChannelExec) channel; chexec.setInputStream(null); chexec.setPty(true); // Allocate pseudo-tty (same as "ssh -t") if (debug) { // Show only in debug mode chexec.setErrStream(System.err); chexec.setOutputStream(System.out); } // Connect channel chexec.connect(); // Read input String result = readChannel(true); disconnect(false); // Disconnect and get exit code return result; } catch (Exception e) { e.printStackTrace(); return null; } }
Example 8
Source File: SshProvider.java From parallec with Apache License 2.0 | 4 votes |
/** * Seems there are bad naming in the library the sysout is in * channel.getInputStream(); the syserr is in * ((ChannelExec)channel).setErrStream(os); * * @param channel * the channel * @return the response on singe request */ public ResponseOnSingeRequest executeAndGenResponse(ChannelExec channel) { ResponseOnSingeRequest sshResponse = new ResponseOnSingeRequest(); InputStream in = null; OutputStream outputStreamStdErr = new ByteArrayOutputStream(); StringBuilder sbStdOut = new StringBuilder(); try { in = channel.getInputStream(); channel.setErrStream(outputStreamStdErr); byte[] tmp = new byte[ParallecGlobalConfig.sshBufferSize]; while (true) { while (in.available() > 0) { int i = in.read(tmp, 0, ParallecGlobalConfig.sshBufferSize); if (i < 0) break; sbStdOut.append(new String(tmp, 0, i)); } if (channel.isClosed()) { if (in.available() > 0) continue; sshResponse.setFailObtainResponse(false); // exit 0 is good int exitStatus = channel.getExitStatus(); sshResponse.setStatusCodeInt(exitStatus); sshResponse.setStatusCode(Integer.toString(exitStatus)); break; } Thread.sleep(ParallecGlobalConfig.sshSleepMIllisBtwReadBuffer); } sshResponse.setResponseBody(sbStdOut.toString()); sshResponse.setErrorMessage(outputStreamStdErr.toString()); sshResponse.setReceiveTimeNow(); } catch (Exception t) { throw new RuntimeException(t); } return sshResponse; }