Refine search
session.connect(); Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream(); channel.connect(); in.read(buf, 0, 5); if (in.read(buf, 0, 1) < 0) { in.read(buf, i, 1); if (buf[i] == (byte) 0x0a) { file = new String(buf, 0, i, StandardCharsets.UTF_8); in.close(); out.close(); session.disconnect(); } catch (Exception e) { throw 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(); }
session.connect(); Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); channel.setInputStream(null); ((ChannelExec) channel).setErrStream(System.err); InputStream in = channel.getInputStream(); InputStream err = ((ChannelExec) channel).getErrStream(); channel.connect(); while (err.available() > 0) { int i = err.read(tmp, 0, 1024); if (i < 0) channel.disconnect(); session.disconnect(); return new SSHClientOutput(exitCode, text.toString()); } catch (Exception e) {
/** * 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); } }
/** * 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); } }
public void execute(String command) throws Exception { JSch jsch = new JSch(); Session session = jsch.getSession(username, host, 22); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); channel.setInputStream(null); InputStream in = channel.getInputStream(); channel.connect(); while (in.available() > 0) { int i = in.read(tmp, 0, 1024); if (i < 0) break; if (channel.isClosed()) { if (in.available() > 0) continue; if (channel.getExitStatus() != 0) throw new Exception("exitStatus:" + channel.getExitStatus());
channel = session.openChannel( "exec" ); ( ( ChannelExec ) channel ).setCommand( exec ); out = channel.getOutputStream(); in = channel.getInputStream(); channel.connect(); try { if ( in != null ) { in.close();
private void checkAck(ChannelExec channel, InputStream in) throws IOException { int returnCode = in.read(); if (returnCode != 0) { channel.disconnect(); throw new RuntimeException("SCP failed, error code: " + returnCode); } }
try { in = channel.getInputStream(); channel.setErrStream(outputStreamStdErr); while (in.available() > 0) { int i = in.read(tmp, 0, ParallecGlobalConfig.sshBufferSize); if (i < 0) break; if (channel.isClosed()) { if (in.available() > 0) continue; sshResponse.setFailObtainResponse(false); int exitStatus = channel.getExitStatus(); sshResponse.setStatusCodeInt(exitStatus); sshResponse.setStatusCode(Integer.toString(exitStatus));
//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
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();
@Override public void close() throws IOException { channel.disconnect(); channelInputStream.close(); }
JSch jsch = new JSch(); Session session = jsch.getSession(userName, host, 22); session.setPassword(password); session.setServerAliveInterval(10000); session.setConfig(config); session.connect(0); ChannelExec channel = (ChannelExec) session.openChannel("exec"); InputStream commandOutput = channel.getInputStream(); channel.setCommand(command); channel.connect(); int readByte = commandOutput.read(); char toAppend = ' '; while (readByte != 0xffffffff) { toAppend = (char) readByte; outputBuffer.append(toAppend); readByte = commandOutput.read(); if (toAppend == '\n') { log.info(print.toString()); channel.disconnect(); session.disconnect(); return outputBuffer.toString();
java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); JSch jsch = new JSch(); Session session = jsch.getSession(user, host, 22); session.setPassword(password); session.setConfig(config); System.out.println("Connected"); ChannelExec channel = (ChannelExec)session.openChannel("exec"); OutputStream o = channel.getOutputStream(); PrintWriter pw = new PrintWriter(o); InputStream in = channel.getInputStream(); ((ChannelExec) channel).setCommand(command1); channel.connect(); int len = in.read(buf); System.out.println(new String(buf,0,len)); channel.disconnect(); session.disconnect();
public String sendCommand(String command) { StringBuilder outputBuffer = new StringBuilder(); try { Channel channel = sesConnection.openChannel("exec"); ((ChannelExec) channel).setCommand(command); InputStream commandOutput = channel.getInputStream(); channel.connect(); int readByte = commandOutput.read(); while (readByte != 0xffffffff) { outputBuffer.append((char) readByte); readByte = commandOutput.read(); } channel.disconnect(); } catch (IOException ioX) { logWarning(ioX.getMessage()); LOGGER.error("error while getting kernelversion from SSH", ioX); return null; } catch (JSchException jschX) { LOGGER.error( "error while getting kernelversion from JSchException SSH", jschX); logWarning(jschX.getMessage()); return null; } return outputBuffer.toString(); }
JSch jsch = new JSch(); Session session = jsch.getSession(user, host, 22); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream(); channel.connect(); in.read(buf, 0, 5); if (in.read(buf, 0, 1) < 0) { in.read(buf, i, 1); if (buf[i] == (byte) 0x0a) { file = new String(buf, 0, i);
Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession(userName, vmIpAddress, 22); session.setPassword(password); session.setConfig(config); session.connect(); Channel channel = session.openChannel("exec"); log.info("Executing sudo command: {}, on {}", command, vmIpAddress); ((ChannelExec) channel).setCommand("sudo -S -p '' " + command); try (OutputStream out = channel.getOutputStream()) { ((ChannelExec) channel).setPty(true); channel.connect(); out.write((password + "\n").getBytes()); out.flush();
JSch jsch = new JSch(); Session session = jsch.getSession(username, hostname, 22); session.setPassword(password); session.setConfig(prop); session.connect(); session.openChannel("exec"); InputStream inputStream = channelssh.getInputStream(); channelssh.setCommand(command); channelssh.connect(); channelssh.disconnect();
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(); }
session.connect(); Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream(); channel.connect(); channel.disconnect(); session.disconnect(); } catch (Exception e) { throw e;