void execSCPWith(ScpCommandLine commandLine) throws SSHException { scp = host.startSession().exec(commandLine.toCommandLine()); }
@Test public void shouldNotThrowTimeoutOnDisconnect() throws IOException { fixture.getClient().authPassword("u", "u"); Session session = fixture.getClient().startSession(); session.allocateDefaultPTY(); Session.Shell shell = session.startShell(); session.close(); fixture.getClient().disconnect(); } }
@Test public void shouldCorrectlyHandleSessionChannelEof() throws IOException, InterruptedException { fixture.setupConnectedDefaultClient().authPassword("jeroen", "jeroen"); Session session = fixture.getClient().startSession(); session.allocateDefaultPTY(); session.close(); Thread.sleep(1000); assertThat("Should still be connected", fixture.getClient().isConnected()); }
/** * 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"); }
@Override public void clear() throws TransportException, ConnectionException { if (session != null) session.close(); }
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(); } }
mockClient.isConnected(); expectLastCall().andReturn(true); mockClient.startSession(); expectLastCall().andReturn(session); session.allocatePTY("vt100", 80, 24, 0, 0, ImmutableMap.<PTYMode, Integer> of()); expectLastCall(); session.exec("some-command"); expectLastCall().andReturn( command ); session.close(); expectLastCall(); command.join(0, TimeUnit.MILLISECONDS); expectLastCall(); command.getInputStream(); expectLastCall().andReturn(is);
try { session = client.startSession(); session.allocateDefaultPTY(); final String command = sb.append("echo ").append("\"") .append(sudoPassword).append("\"") final String rebootCmdLogger = "echo \"??SUDO_PW??\" | sudo -S /sbin/shutdown -r now"; LOGGER.info("Sending reboot command: {}", rebootCmdLogger); Command cmd = session.exec(command); try { cmd.join(); session.join(); } catch (ConnectionException e) { LOGGER.debug("ConnectException while sending reboot command. Probably system is going down...", e); session.allocateDefaultPTY(); cmd = session.exec("reboot"); try { cmd.join();
// 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(); }
session.exec("cd " + app.getStaticWorkingDirectory()); for (Entry<String, String> entry : nv.entrySet()) { log.info("Env[" + entry.getKey() + "] = " + entry.getValue()); session.setEnvVar(entry.getKey(), entry.getValue()); Command cmd = session.exec(command); log.info("stdout=" + GfacUtils.readFromStream(session.getInputStream())); cmd.join(COMMAND_EXECUTION_TIMEOUT, TimeUnit.SECONDS);
protected void allocatePTY(Session s) throws ConnectionException, TransportException { // this was set as the default, but it makes output harder to read // and causes stderr to be sent to stdout; // but some systems requiretty for sudoing if (allocatePTY) s.allocatePTY("vt100", 80, 24, 0, 0, Collections.<PTYMode, Integer> emptyMap()); // s.allocatePTY("dumb", 80, 24, 0, 0, Collections.<PTYMode, Integer> emptyMap()); }
checkConnection(); try (Session session = sshClient.startSession(); SessionChannel shell = (SessionChannel) session.startShell(); PrintWriter commandWriter = new PrintWriter(shell.getOutputStream())) { outputHandler.handleStdOut(shell.getInputStream()); while (true) { try { session.join(5, TimeUnit.SECONDS); } catch (ConnectionException e) { boolean isInterrupted = ExceptionUtils.indexOfType(e, InterruptedException.class) >= 0;
session = acquire(newSessionAction()); shell = session.startShell();
@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."); } }
/** * 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 void clear() throws TransportException, ConnectionException { if (session != null) session.close(); }
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); } }
try { session = client.startSession(); session.allocateDefaultPTY(); final String command = sb.append("echo ").append("\"") .append(sudoPassword).append("\"") final String haltCmdLogger = "echo \"??SUDO_PW??\" | sudo -S /sbin/shutdown -h now"; LOGGER.info("Sending halt command: {}", haltCmdLogger); Command cmd = session.exec(command); try { cmd.join(); session.join(); } catch (ConnectionException e) { LOGGER.debug("ConnectException while sending halt command. Probably system is going down...", e); session.allocateDefaultPTY(); LOGGER.warn("Sudo unknown: Trying \"halt\"..."); cmd = session.exec("halt"); try { cmd.join();
public static void scpDownload(HostInfo hostInfo, FromTo fromTo) throws IOException { SSHClient ssh = getSshClient(hostInfo); try { Session session = ssh.startSession(); session.allocateDefaultPTY(); try { ssh.newSCPFileTransfer().download(fromTo.from, fromTo.to); } finally { session.close(); } } finally { ssh.disconnect(); ssh.close(); } }