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"); } }
void exit() { if (scp != null) { IOUtils.closeQuietly(scp); if (scp.getExitStatus() != null) { exitStatus = scp.getExitStatus(); if (scp.getExitStatus() != 0) log.warn("SCP exit status: {}", scp.getExitStatus()); } else { exitStatus = -1; } if (scp.getExitSignal() != null) { log.warn("SCP exit signal: {}", scp.getExitSignal()); } } scp = null; }
@Test public void shouldExecuteBackgroundCommand() throws IOException { SSHClient sshClient = fixture.setupConnectedDefaultClient(); sshClient.authPassword("jeroen", "jeroen"); File file = new File(temp.getRoot(), "testdir"); assertThat("File should not exist", !file.exists()); // TODO figure out why this does not really execute in the background. Session.Command exec = sshClient.startSession().exec("mkdir " + file.getPath() + " &"); exec.join(); assertThat("File should exist", file.exists()); assertThat("File should be directory", file.isDirectory()); SFTPClient sftpClient = sshClient.newSFTPClient(); if (sftpClient.statExistence("&") != null) { sftpClient.rmdir("&"); // TODO fail here when this is fixed } } }
+ " | sed -n 2p"; final Session.Command cmd = session.exec(cmdString); cmd.join(30, TimeUnit.SECONDS); final String output = IOUtils.readFully( cmd.getInputStream()).toString(); LOGGER.debug("Output of ip query: {}", output); final Matcher m = IPADDRESS_PATTERN.matcher(output); + " | grep \"inet addr\""; final Session.Command ifCfgCmd = session.exec(ifConfigCmd); ifCfgCmd.join(30, TimeUnit.SECONDS); final String ifconfigOutput = IOUtils.readFully( ifCfgCmd.getInputStream()).toString(); LOGGER.debug("Output of ifconfig query: {}.", ifconfigOutput);
cmd.join(COMMAND_EXECUTION_TIMEOUT, TimeUnit.SECONDS); if (cmd.getExitStatus() != 0) { log.error("Process finished with non zero return value. Process may have failed"); } else {
Command cmd = session.exec(command); try { cmd.join(); session.join(); } catch (ConnectionException e) { return; if (cmd.getExitStatus() != null && cmd.getExitStatus() != 0) { LOGGER.warn("Sudo unknown: Trying \"reboot\"..."); cmd = session.exec("reboot"); try { cmd.join(); LOGGER.debug("join successful after 'reboot'."); } catch (ConnectionException e) {
Command cmd = session.exec(command); try { cmd.join(); session.join(); } catch (ConnectionException e) { return; if (cmd.getExitStatus() != null && cmd.getExitStatus() != 0) { cmd = session.exec("halt"); try { cmd.join(); LOGGER.debug("join successful after 'halt'."); } catch (ConnectionException e) {
new InputStreamReader(new StreamGobbler(exec.getInputStream()), Charset.forName(session.getHost().getEncoding()))); final BufferedReader stderrReader = new BufferedReader( new InputStreamReader(new StreamGobbler(exec.getErrorStream()), Charset.forName(session.getHost().getEncoding())));
command.join(); final Integer exitStatus = command.getExitStatus(); if (exitStatus != PUPPET_FINISHED_WITH_NO_FAILURES && exitStatus != 0) { throw new RuntimeException(String.format("Failed to execute puppet. " + "Exit code: %d. Exit message: %s", exitStatus, command.getExitErrorMessage()));
cmd.join(30, TimeUnit.SECONDS); String output = IOUtils.readFully(cmd.getInputStream()).toString(); return this.parseFrequency(output); } catch (IOException e) {
final String cmdString = "LC_ALL=C /usr/bin/whereis " + executableBinary; final Session.Command cmd = session.exec(cmdString); cmd.join(30, TimeUnit.SECONDS); final Integer exitStatus = cmd.getExitStatus(); String output = IOUtils.readFully(cmd.getInputStream()) .toString(); if (exitStatus == 0) {
final Session.Command cmd = session.exec("ls -a"); System.out.println(IOUtils.readFully(cmd.getInputStream()).toString()); cmd.join(5, TimeUnit.SECONDS); System.out.println("\n** exit status: " + cmd.getExitStatus()); } finally { session.close();
final String cmdString = "ls -1 /sys/class/net"; final Session.Command cmd = session.exec(cmdString); cmd.join(30, TimeUnit.SECONDS); final String output = IOUtils.readFully( cmd.getInputStream()).toString(); final String[] lines = output.split("\n"); final List<String> interfaces = new ArrayList<String>();
outgobbler = new StreamGobbler(output.getInputStream(), out, (Logger)null); outgobbler.start(); errgobbler = new StreamGobbler(output.getErrorStream(), err, (Logger)null); errgobbler.start(); output.join((int)Math.min(timeout.toMilliseconds(), Integer.MAX_VALUE), TimeUnit.MILLISECONDS); return output;
/** * Queries the current CPU temperature. * * @param vcgencmdPath the path to vcgencmd * @return the temperature in Celsius * @throws RaspiQueryException if something goes wrong */ private Double queryCpuTemp(String vcgencmdPath) throws RaspiQueryException { if (client != null) { if (client.isConnected() && client.isAuthenticated()) { Session session; try { session = client.startSession(); final String cmdString = vcgencmdPath + " measure_temp"; final Command cmd = session.exec(cmdString); cmd.join(30, TimeUnit.SECONDS); String output = IOUtils.readFully(cmd.getInputStream()).toString(); return this.parseTemperature(output); } catch (IOException e) { throw RaspiQueryException.createTransportFailure(hostname, e); } } else { throw new IllegalStateException("You must establish a connection first."); } } else { throw new IllegalStateException("You must establish a connection first."); } }
public void makeDirectory(InvocationContext context) throws ProviderException { ApplicationDeploymentDescriptionType app = context.getExecutionDescription().getApp().getType(); Session session = null; try { session = getSession(context); StringBuilder commandString = new StringBuilder(); commandString.append("mkdir -p "); commandString.append(app.getScratchWorkingDirectory()); commandString.append(" ; "); commandString.append("mkdir -p "); commandString.append(app.getStaticWorkingDirectory()); commandString.append(" ; "); commandString.append("mkdir -p "); commandString.append(app.getInputDataDirectory()); commandString.append(" ; "); commandString.append("mkdir -p "); commandString.append(app.getOutputDataDirectory()); Command cmd = session.exec(commandString.toString()); cmd.join(COMMAND_EXECUTION_TIMEOUT, TimeUnit.SECONDS); } catch (ConnectionException e) { throw new ProviderException(e.getMessage(), e); } catch (TransportException e) { throw new ProviderException(e.getMessage(), e); } catch (IOException e) { throw new ProviderException(e.getMessage(), e); } finally { closeSession(session); } }
@Override public String run(String command, int timeout) throws RaspiQueryException { LOGGER.info("Running custom command: {}", command); if (client != null) { if (client.isConnected() && client.isAuthenticated()) { Session session; try { session = client.startSession(); session.allocateDefaultPTY(); final Command cmd = session.exec(command); cmd.join(timeout, TimeUnit.SECONDS); cmd.close(); final String output = IOUtils.readFully(cmd.getInputStream()).toString(); final String error = IOUtils.readFully(cmd.getErrorStream()).toString(); final StringBuilder sb = new StringBuilder(); final String out = sb.append(output).append(error).toString(); LOGGER.debug("Output of '{}': {}", command, out); session.close(); return out; } catch (IOException e) { throw RaspiQueryException.createTransportFailure(hostname, e); } } else { throw new IllegalStateException("You must establish a connection first."); } } else { throw new IllegalStateException("You must establish a connection first."); } }
/** * Checks if the specified interface has a carrier up and running via * "cat /sys/class/net/[interface]/carrier". * * @param interfaceName the interface to check * @return true, when the interface has a carrier up and running * @throws RaspiQueryException if something goes wrong */ private boolean checkCarrier(String interfaceName) throws RaspiQueryException { LOGGER.info("Checking carrier of {}...", interfaceName); Session session; try { session = getSSHClient().startSession(); final String cmdString = "cat /sys/class/net/" + interfaceName + "/carrier"; final Session.Command cmd = session.exec(cmdString); cmd.join(30, TimeUnit.SECONDS); final String output = IOUtils.readFully(cmd.getInputStream()).toString(); if (output.contains("1")) { LOGGER.debug("{} has a carrier up and running.", interfaceName); return true; } else { LOGGER.debug("{} has no carrier.", interfaceName); return false; } } catch (IOException e) { throw RaspiQueryException.createTransportFailure(e); } }
session.exec("some-command"); expectLastCall().andReturn( command ); session.close(); expectLastCall(); command.join(0, TimeUnit.MILLISECONDS); expectLastCall(); command.getInputStream(); expectLastCall().andReturn(is); command.getErrorStream(); expectLastCall().andReturn(is); command.getExitStatus(); expectLastCall().andReturn(null); replay(mockConnection); replay(mockClient);
outgobbler = new StreamGobbler(output.getInputStream(), out, (Logger)null); outgobbler.start(); errgobbler = new StreamGobbler(output.getErrorStream(), err, (Logger)null); errgobbler.start(); output.join(sshClientConnection.getSessionTimeout(), TimeUnit.MILLISECONDS); return output;