@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()); }
@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(); } }
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(); } }
/** * 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"); }
try { Session session = getSSHClient().startSession(); session.allocateDefaultPTY(); final String cmdString = "LC_ALL=C /usr/bin/whereis " + executableBinary; final Session.Command cmd = session.exec(cmdString);
try { session = getSSHClient().startSession(); session.allocateDefaultPTY(); final String cmdString = "ip -f inet addr show dev " + name + " | sed -n 2p"; name); session = getSSHClient().startSession(); session.allocateDefaultPTY(); final String ifConfigCmd = "/sbin/ifconfig " + name + " | grep \"inet addr\"";
try { session = client.startSession(); session.allocateDefaultPTY(); final String command = sb.append("echo ").append("\"") .append(sudoPassword).append("\"") session.allocateDefaultPTY(); cmd = session.exec("reboot"); try {
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("\"") session.allocateDefaultPTY(); LOGGER.warn("Sudo unknown: Trying \"halt\"..."); cmd = session.exec("halt");
@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."); } }
@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); } }
/** * Upload one or more files via the same SSH/SCP connection to a remote host. */ public static void scpUpload(HostInfo hostInfo, List<FromTo> fromTos) throws IOException { SSHClient ssh = getSshClient(hostInfo); try { Session session = ssh.startSession(); session.allocateDefaultPTY(); try { for (FromTo ft: fromTos) { System.out.format("SCP cp %s -> %s/%s%n", ft.from, hostInfo.host, ft.to); ssh.newSCPFileTransfer().upload(ft.from, ft.to); } } finally { session.close(); } } finally { ssh.disconnect(); ssh.close(); } }
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(); } }
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(); } }
private void logIntoIp(String ipAddress) throws IOException, InterruptedException { final SSHClient ssh = new SSHClient(); OpenSSHKeyFile k = new OpenSSHKeyFile(); k.init(new File(dotHaruhi, keyPairName + ".pem")); ssh.addHostKeyVerifier(new PromiscuousVerifier()); ssh.connect(ipAddress); try { ssh.authPublickey(clusterUsername, k); Session that = ssh.startSession(); try { that.allocateDefaultPTY(); Session.Shell shell = that.startShell(); new StreamCopier(shell.getInputStream(), System.out) .bufSize(shell.getLocalMaxPacketSize()) .spawn("stdout"); new StreamCopier(shell.getErrorStream(), System.err) .bufSize(shell.getLocalMaxPacketSize()) .spawn("stderr"); new StreamCopier(System.in, shell.getOutputStream()) .bufSize(shell.getRemoteMaxPacketSize()) .copy(); } finally { that.close(); } } finally { ssh.close(); } }