@Override public void close() { try { if (sshClient != null) { sshClient.close(); sshClient = null; } } catch (IOException e) { log.warn("Could not close ssh executor", e); } }
@Override public void disconnect() { try { client.close(); } catch(IOException e) { log.warn(String.format("Ignore disconnect failure %s", e.getMessage())); } super.disconnect(); }
} finally { session.close(); ssh.close(); expect.close();
/** * 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(); } }