@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 } } }
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());
sshCmd.join(timeout, TimeUnit.SECONDS); String output = IOUtils.readFully(sshCmd.getInputStream()).toString(); String stderr = IOUtils.readFully(sshCmd.getErrorStream()).toString();
public static int executeCommand(HostInfo hostInfo, String command, boolean debugOutputEnabled) throws IOException { SSHClient ssh = getSshClient(hostInfo); try { Session session = ssh.startSession(); session.allocateDefaultPTY(); try { if (debugOutputEnabled) { System.out.println("About to run: " + command); } Command cmd = session.exec(command); readCommandOutput(cmd); cmd.join(); printExitCode(cmd.getExitStatus()); return cmd.getExitStatus(); } finally { session.close(); } } finally { ssh.disconnect(); ssh.close(); } }
/** * Checks if the path is a correct path to vcgencmd. * * @param path the path to check * @param client authenticated and open client * @return true, if correct, false if not * @throws IOException if something ssh related goes wrong */ private boolean isValidVcgencmdPath(String path, SSHClient client) throws IOException { final Session session = client.startSession(); session.allocateDefaultPTY(); LOGGER.debug("Checking vcgencmd location: {}", path); final Command cmd = session.exec(path); cmd.join(30, TimeUnit.SECONDS); session.close(); final Integer exitStatus = cmd.getExitStatus(); final String output = IOUtils.readFully(cmd.getInputStream()).toString().toLowerCase(); LOGGER.debug("Path check output: {}", output); return exitStatus != null && exitStatus.equals(0) && !output.contains("not found") && !output.contains("no such file or directory"); }
public void sshExec(String cmd) throws IOException { @SuppressWarnings("resource") final SSHClient ssh = new SSHClient(); ssh.addHostKeyVerifier(new PromiscuousVerifier()); try { ssh.connect("localhost", vm.getSshForwardPort()); ssh.authPassword("root", ""); try (Session session = ssh.startSession()) { final Session.Command sessionCmd = session.exec(cmd); sessionCmd.join(5, TimeUnit.SECONDS); int exitCode = sessionCmd.getExitStatus(); if (exitCode != 0) { throw new RuntimeException("command " + cmd + " returned exit status code " + exitCode); } } } finally { if (ssh.isConnected()) { ssh.disconnect(); } } }
@Override public Integer executeCommand(String command, OutputHandler outputHandler) { checkConnection(); try (Session session = sshClient.startSession(); Session.Command sshCommand = session.exec(command)) { sshCommand.join(); outputHandler.handleStdOut(sshCommand.getInputStream()); outputHandler.handleStdErr(sshCommand.getErrorStream()); if (sshCommand.getExitStatus() == null || sshCommand.getExitStatus() != 0) { log.error("Execute command [{}] finished with error status [{}]", command, sshCommand.getExitStatus()); } else { log.info("Execute command [{}] finished normally", command); } return sshCommand.getExitStatus(); } catch (ConnectionException | TransportException e) { ExceptionUtil.checkInterrupted(e); throw new ArtifactConnectException("Cannot execute command [" + command + "], encountered connection error", 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 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 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 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); } }
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); } }
@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(); } }
@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 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 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(); } }
@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); } }