String readMessage() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); int x; while ((x = scp.getInputStream().read()) != LF) { if (x == -1) { if (baos.size() == 0) { return ""; } else { throw new IOException("EOF while reading message"); } } else { baos.write(x); } } final String msg = baos.toString(IOUtils.UTF8.displayName()); log.debug("Read message: `{}`", msg); return msg; }
void check(String what) throws IOException { int code = scp.getInputStream().read(); switch (code) { case -1: String stderr = IOUtils.readFully(scp.getErrorStream(), loggerFactory).toString(); if (!stderr.isEmpty()) stderr = ". Additional info: `" + stderr + "`"; throw new SCPException("EOF while expecting response to protocol message" + stderr); case 0: // OK log.debug(what); return; case 1: // Warning? not case 2: final String remoteMessage = readMessage(); throw new SCPRemoteException("Remote SCP command had error: " + remoteMessage, remoteMessage); default: throw new SCPException("Received unknown response code"); } }
Session session = ssh.startSession(); Session.Command cmd = session.exec("ls -l"); System.out.println(IOUtils.readFully(cmd.getInputStream()).toString()); cmd.join(10, TimeUnit.SECONDS); session = ssh.startSession(); Session.Command cmd2 = session.exec("ls -a"); System.out.println(IOUtils.readFully(cmd2.getInputStream()).toString());
@Override public ExecChannel create() throws Exception { session = SessionChannel.class.cast(acquire(noPTYConnection())); output = session.exec(command); return new ExecChannel(output.getOutputStream(), output.getInputStream(), output.getErrorStream(), new Supplier<Integer>() { @Override public Integer get() { return output.getExitStatus(); } }, new Closeable() { @Override public void close() throws IOException { clear(); } }); }
@Override public Double run() throws RaspiQueryException { LOGGER.info("Querying load average for time period {}", this.period); Session session; try { session = getSSHClient().startSession(); session.allocateDefaultPTY(); final Command cmd = session.exec(LOAD_AVG_CMD); cmd.join(30, TimeUnit.SECONDS); cmd.close(); final String output = IOUtils.readFully(cmd.getInputStream()) .toString(); return this.parseLoadAverage(output, this.period); } catch (IOException e) { throw RaspiQueryException.createTransportFailure(e); } }
@Override public ExecChannel create() throws Exception { session = SessionChannel.class.cast(acquire(noPTYConnection())); output = session.exec(command); return new ExecChannel(output.getOutputStream(), output.getInputStream(), output.getErrorStream(), new Supplier<Integer>() { @Override public Integer get() { return output.getExitStatus(); } }, new Closeable() { @Override public void close() throws IOException { clear(); } }); }
@Override public ExecResponse create() throws Exception { try { session = acquire(execConnection()); Command output = session.exec(checkNotNull(command, "command")); String outputString = IOUtils.readFully(output.getInputStream()).toString(); output.join(sshClientConnection.getSessionTimeout(), TimeUnit.MILLISECONDS); String errorString = IOUtils.readFully(output.getErrorStream()).toString(); return new ExecResponse(outputString, errorString, output.getExitStatus()); } finally { clear(); } }
@Override public ExecResponse create() throws Exception { try { session = acquire(execConnection()); Command output = session.exec(checkNotNull(command, "command")); String outputString = IOUtils.readFully(output.getInputStream()).toString(); output.join(sshClientConnection.getSessionTimeout(), TimeUnit.MILLISECONDS); int errorStatus = output.getExitStatus(); String errorString = IOUtils.readFully(output.getErrorStream()).toString(); return new ExecResponse(outputString, errorString, errorStatus); } finally { clear(); } }
@Override public Double run() throws RaspiQueryException { LOGGER.info("Querying uptime..."); try { final Session session = getSSHClient().startSession(); final Command cmd = session.exec(UPTIME_CMD); cmd.join(30, TimeUnit.SECONDS); final String output = IOUtils.readFully(cmd.getInputStream()) .toString(); return this.formatUptime(output); } catch (IOException e) { throw RaspiQueryException.createTransportFailure(e); } }
@Override public ExecResponse create() throws Exception { try { session = acquire(execConnection()); Command output = session.exec(checkNotNull(command, "command")); String outputString = IOUtils.readFully(output.getInputStream()).toString(); output.join(sshClientConnection.getSessionTimeout(), TimeUnit.MILLISECONDS); int errorStatus = output.getExitStatus(); String errorString = IOUtils.readFully(output.getErrorStream()).toString(); return new ExecResponse(outputString, errorString, errorStatus); } finally { clear(); } }
@Override public ExecResponse create() throws Exception { try { session = acquire(execConnection()); Command output = session.exec(checkNotNull(command, "command")); String outputString = IOUtils.readFully(output.getInputStream()).toString(); output.join(sshClientConnection.getSessionTimeout(), TimeUnit.MILLISECONDS); int errorStatus = output.getExitStatus(); String errorString = IOUtils.readFully(output.getErrorStream()).toString(); return new ExecResponse(outputString, errorString, errorStatus); } finally { clear(); } }
@Override public String run() throws RaspiQueryException { LOGGER.info("Querying serial number..."); try { Session session = getSSHClient().startSession(); final Command cmd = session.exec(CAT_PROC_CPUINFO_GREP_SERIAL); cmd.join(30, TimeUnit.SECONDS); String output = IOUtils.readFully(cmd.getInputStream()).toString(); return this.formatCpuSerial(output); } catch (IOException e) { throw RaspiQueryException.createTransportFailure(e); } }
@Override public ExecResponse create() throws Exception { try { session = acquire(execConnection()); Command output = session.exec(checkNotNull(command, "command")); String outputString = IOUtils.readFully(output.getInputStream()).toString(); output.join(sshClientConnection.getSessionTimeout(), TimeUnit.MILLISECONDS); String errorString = IOUtils.readFully(output.getErrorStream()).toString(); return new ExecResponse(outputString, errorString, output.getExitStatus()); } finally { clear(); } }
long transferFromRemote(StreamCopier.Listener listener, OutputStream dest, long length) throws IOException { return new StreamCopier(scp.getInputStream(), dest) .bufSize(scp.getLocalMaxPacketSize()).length(length) .keepFlushing(false) .listener(listener) .copy(); }
public CommandMocker withResponse(String output) { Mockito.when(command.getInputStream()).thenReturn( IOUtils.toInputStream(output)); return this; }
@Override public RaspiMemoryBean run() throws RaspiQueryException { LOGGER.info("Querying memory information..."); try { Session session = getSSHClient().startSession(); final Session.Command cmd = session.exec(MEMORY_INFO_CMD); cmd.join(30, TimeUnit.SECONDS); return this.formatMemoryInfo(IOUtils.readFully(cmd.getInputStream()).toString()); } catch (IOException e) { throw RaspiQueryException.createTransportFailure(e); } }
long transferFromRemote(StreamCopier.Listener listener, OutputStream dest, long length) throws IOException { return new StreamCopier(scp.getInputStream(), dest, loggerFactory) .bufSize(scp.getLocalMaxPacketSize()).length(length) .keepFlushing(false) .listener(listener) .copy(); }
/** * Stream command output as log message for easy debugging */ public static void logCommandOutput(Logger logger, String instanceId, Session.Command command) { final Marker marker = MarkerFactory.getMarker("ssh-" + instanceId); new InfoStreamLogger(command.getInputStream(), logger, marker) .start(); new ErrorStreamLogger(command.getErrorStream(), logger, marker) .start(); }
String output = IOUtils.readFully(sshCmd.getInputStream()).toString(); String stderr = IOUtils.readFully(sshCmd.getErrorStream()).toString(); ret.setReturnCode(sshCmd.getExitStatus());
long transferFromRemote(StreamCopier.Listener listener, OutputStream dest, long length) throws IOException { return new StreamCopier(scp.getInputStream(), dest, loggerFactory) .bufSize(scp.getLocalMaxPacketSize()).length(length) .keepFlushing(false) .listener(listener) .copy(); }