public static ByteArrayOutputStream readFully(InputStream stream) throws IOException { return readFully(stream, LoggerFactory.DEFAULT); }
public static String readFromFile(File f) throws IOException { FileInputStream fileInputStream = new FileInputStream(f); try { ByteArrayOutputStream byteArrayOutputStream = IOUtils.readFully(fileInputStream); return byteArrayOutputStream.toString(IOUtils.UTF8.displayName()); } finally { IOUtils.closeQuietly(fileInputStream); } } }
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"); } }
public static ByteArrayOutputStream readFully(InputStream stream) throws IOException { return readFully(stream, LoggerFactory.DEFAULT); }
void check(String what) throws IOException { int code = scp.getInputStream().read(); switch (code) { case -1: String stderr = IOUtils.readFully(scp.getErrorStream()).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: throw new SCPException("Remote SCP command had error: " + readMessage()); default: throw new SCPException("Received unknown response code"); } }
@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 String run() throws RaspiQueryException { LOGGER.debug("Querying system time via 'date --rfc-2822'."); try { Session session = getSSHClient().startSession(); String cmdString = "date --rfc-2822"; final Session.Command cmd = session.exec(cmdString); cmd.join(30, TimeUnit.SECONDS); String output = IOUtils.readFully(cmd.getInputStream()) .toString(); final String result = output.trim(); LOGGER.debug("System time: {}", result); return result; } 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 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(); } }
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"); } }
@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 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); } }
@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 String run() throws RaspiQueryException { LOGGER.debug("Querying firmware version, vcgencmd path={}", this.vcgencmdPath); try { Session session = getSSHClient().startSession(); String cmdString = vcgencmdPath + " version"; final Session.Command cmd = session.exec(cmdString); cmd.join(30, TimeUnit.SECONDS); String output = IOUtils.readFully(cmd.getInputStream()) .toString(); final String result = this.parseFirmwareVersion(output); LOGGER.debug("Firmware version: {}", result); return result; } catch (IOException e) { throw RaspiQueryException.createTransportFailure(e); } }
@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); } }
private WlanBean queryWirelessInterfaceWithIwconfig(String interfaceName, String iwconfigPath) throws RaspiQueryException { LOGGER.info("Executing {} to query wireless interface '{}'...", iwconfigPath, interfaceName); Session session; try { session = getSSHClient().startSession(); session.allocateDefaultPTY(); final String cmdString = "LC_ALL=C " + iwconfigPath + " " + interfaceName; final Session.Command cmd = session.exec(cmdString); cmd.join(30, TimeUnit.SECONDS); String output = IOUtils.readFully(cmd.getInputStream()) .toString(); LOGGER.debug("Output of '{}': \n{}", cmdString, output); return this.parseIwconfigOutput(output); } catch (IOException e) { throw RaspiQueryException.createTransportFailure(e); } }
/** * Execute a command on remote host * @param command command to execute * @param wait do we wait for command termination * @return output * @throws IOException */ public String executeCommand(String command, Integer wait) throws IOException { Session session = ssh.startSession(); String result = "NO_RESULT"; try { Command cmd = session.exec(command); result = IOUtils.readFully(cmd.getInputStream()).toString(); cmd.join(wait, TimeUnit.SECONDS); } finally { session.close(); } return result; } }
@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); } }