public SSHClient close() { try { Utils.close(out); channel.close(false); session.close(false); client.stop(); return this; } finally { this.client = null; this.channel = null; this.session = null; this.out = null; } } }
session.authPassword("root", ""); ChannelShell channel = (ChannelShell)session.createShellChannel(); channel.setPtyModes(tty);
@Override public AuthFuture authenticate(final ClientSession session) throws IOException { session.addPasswordIdentity(password); return session.auth(); } }
sshSession.authPassword(username, pwd); int ret = sshSession.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0); if ((ret & ClientSession.AUTHED) == 0) { System.err.println("Password authentication failed"); channel = sshSession.createChannel("exec", cmd); channel.setIn(new ByteArrayInputStream(new byte[0])); } else { channel = sshSession.createChannel("shell"); channel.setIn(new NoCloseInputStream(System.in)); ((ChannelShell) channel).setPtyColumns(ShellUtils.getTermWidth(session)); } finally { session.put(Console.IGNORE_INTERRUPTS, oldIgnoreInterrupts); sshSession.close(false);
@Test public void testClientWithHeartBeat() throws Exception { SshClient client = SshClient.setUpDefaultClient(); client.getProperties().put(ClientFactoryManager.HEARTBEAT_INTERVAL, "500"); client.start(); ClientSession session = client.connect("localhost", port).await().getSession(); session.authPassword("smx", "smx").await().isSuccess(); ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL); int state = channel.waitFor(ClientChannel.CLOSED, 2000); assertTrue((state & ClientChannel.CLOSED) == 0); channel.close(false); client.stop(); }
private static ClientSession connect(SshClient client, final String user, KeyPair keyPair, final String ip, final int port) throws IOException, InterruptedException { ClientSession session = client.connect(user, ip, port).await().getSession(); int authState = ClientSession.WAIT_AUTH; while ((authState & ClientSession.WAIT_AUTH) != 0) { session.addPublicKeyIdentity(keyPair); log.info("Authenticating to " + user + "@" + ip); AuthFuture authFuture = session.auth(); authFuture.addListener(new SshFutureListener<AuthFuture>() { @Override public void operationComplete(AuthFuture authFuture) { log.info("Authentication completed with " + (authFuture.isSuccess() ? "success" : "failure") + " for " + user + "@" + ip + ":" + port); } }); authState = session.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0); } if ((authState & ClientSession.CLOSED) != 0) { throw new IOException("Authentication failed for " + user + "@" + ip); } return session; }
sshClient.connect(EncryptionTool.getInstance().decrypt(username), host, port).await().getSession(); if (password != null) { clientSession.addPasswordIdentity(EncryptionTool.getInstance().decrypt(password)); } else if (keyFile != null) { KeyPairProvider keyPairProvider = new FileKeyPairProvider(new String[]{ }); KeyPair keyPair = keyPairProvider.loadKeys().iterator().next(); clientSession.addPublicKeyIdentity(keyPair); AuthFuture authFuture = clientSession.auth(); authFuture.await(AUTH_TIMEOUT); if (!authFuture.isSuccess()) {
if (session != null){ while ((ret & ClientSession.WAIT_AUTH) != 0) { session.authPassword(login, password); ret = session.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
@Override protected void executeInternal(String command) throws Exception { commandResult = null; // executing new command, reset previous result final ClientChannel channel = session.createChannel("exec", command.concat(NEW_LINE)); try ( InputStream in = new ByteArrayInputStream(new byte[0]); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream() ) { channel.setIn(in); channel.setOut(AnsiConsole.wrapOutputStream(out)); channel.setErr(AnsiConsole.wrapOutputStream(err)); channel.open(); channel.waitFor(ClientChannel.CLOSED, 0); out.writeTo(System.out); err.writeTo(System.err); commandResult = out.toString(); final boolean isError = (channel.getExitStatus() != null && channel.getExitStatus() != 0); if (isError) { log.error(commandResult); throw new IllegalArgumentException(String.format("Operation '%s' failed", command)); } } finally { channel.close(true); } }
private static void doWithScp(String user, String ip, int port, String pemPath, DoWithScpAction doWithScpAction) throws IOException, InterruptedException { SshClient client = SshClient.setUpDefaultClient(); ClientSession session = null; try { client.start(); session = connect(client, user, loadKeyPair(pemPath), ip, port); ScpClient scpClient = session.createScpClient(); doWithScpAction.doScpAction(scpClient); } finally { if (session != null) { session.close(true); } client.close(true); } }
@Override public boolean isConnected() { return (session != null && !session.isClosed()); }
sshSession.authPassword(username, pwd); int ret = sshSession.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0); if ((ret & ClientSession.AUTHED) == 0) { System.err.println("Password authentication failed"); channel = sshSession.createChannel("exec", cmd); channel.setIn(new ByteArrayInputStream(new byte[0])); } else { channel = sshSession.createChannel("shell"); channel.setIn(new NoCloseInputStream(System.in)); ((ChannelShell) channel).setPtyColumns(ShellUtils.getTermWidth(session)); } finally { session.put(Console.IGNORE_INTERRUPTS, oldIgnoreInterrupts); sshSession.close(false);
if (!session.authPassword(user, password).await().isSuccess()) { throw new Exception("Authentication failure"); channel = session.createChannel("exec"); channel.setIn(new ByteArrayInputStream(sb.append("\n").toString().getBytes())); } else { terminal = new TerminalFactory().getTerminal(); channel = session.createChannel("shell"); channel.setIn(System.in); ((ChannelShell) channel).setupSensibleDefaultPty();
/** * Constructor that creates creates the terminal and * connects the I/O streams to the server * @throws IOException */ public SessionTerminal() throws IOException { try { this.terminal = new Terminal(TERM_WIDTH, TERM_HEIGHT); in = new NoClosePipedOutputStream(); out = new NoClosePipedInputStream(); NoClosePipedOutputStream pipedOut = new NoClosePipedOutputStream(out); NoClosePipedInputStream pipedIn = new NoClosePipedInputStream(in); channel = session.createChannel(ClientChannel.CHANNEL_SHELL); channel.setIn(pipedIn); channel.setOut(pipedOut); channel.setErr(pipedOut); new Thread(this).start(); channel.open(); } catch (Exception e) { LoggerFactory.getLogger(getClass()).error(e.getMessage(), e); } }
ConnectFuture future = client.connect(host, Integer.parseInt(port)); ClientSession session = future.await().getSession(); if (!session.authPassword(user, pass).await().isSuccess()) { System.err.println("Authentication failure, aborting."); return; ClientChannel channel = session.createChannel("exec", script); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream();
@Override public void operationComplete(final CloseFuture future) { if (future.isClosed() == false) { session.close(true); } session = null; } });
@Override public AuthFuture authenticate(final ClientSession session) throws IOException { session.addPasswordIdentity(password); return session.auth(); } }
@Override public void operationComplete(final CloseFuture future) { if (future.isClosed() == false) { session.close(true); } session = null; } });