void execSCPWith(ScpCommandLine commandLine) throws SSHException { scp = host.startSession().exec(commandLine.toCommandLine()); }
@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());
logger.trace(String.format("[start SSH] %s", cmd)); sshCmd = session.exec(cmd); sshCmd.join(timeout, TimeUnit.SECONDS); String output = IOUtils.readFully(sshCmd.getInputStream()).toString();
void execSCPWith(List<Arg> args, String path) throws SSHException { final StringBuilder cmd = new StringBuilder(SCP_COMMAND); for (Arg arg : args) cmd.append(" ").append(arg); cmd.append(" "); if (path == null || path.isEmpty()) cmd.append("."); else cmd.append("'").append(path.replaceAll("'", "\\'")).append("'"); scp = host.startSession().exec(cmd.toString()); }
void execSCPWith(ScpCommandLine commandLine) throws SSHException { scp = host.startSession().exec(commandLine.toCommandLine()); }
public SessionMocker withCommand(String cmdString, Command command) { try { Mockito.when(session.exec(Mockito.eq(cmdString))).thenReturn( command); } catch (ConnectionException e) { } catch (TransportException e) { } return this; }
Command cmd = null; try (Session session = sshClient.startSession()) { session.allocateDefaultPTY(); cmd = session.exec(command); new StreamCopier(cmd.getInputStream(), AnsiConsole.out()).keepFlushing(true).copy(); cmd.join(timeout, timeUnit); }finally{ if(cmd != null){ cmd.close(); } }
// Start a new session session = sshClient.startSession(); session.allocatePTY("vt220", 80,24,0,0,Collections.<PTYMode, Integer>emptyMap()); Command cmd = null; String response = null; // your allocating a new session there try (Session session = sshClient.startSession()) { cmd = session.exec("sudo service riak start"); response = IOUtils.readFully(cmd.getInputStream()).toString(); cmd.join(timeout, timeUnit); } finally { if (cmd != null) cmd.close(); }
@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.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 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 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); } }
@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 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 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); } }