SSHClient ssh = new SSHClient(); //ssh.useCompression(); ssh.loadKnownHosts(); ssh.connect("localhost"); try { ssh.authPublickey(System.getProperty("user.name")); new SCPDownloadClient(ssh).copy("ten", "/tmp"); } finally { ssh.disconnect(); }
public SSHClient connectClient(SSHClient client) throws IOException { client.connect(server.getHost(), server.getPort()); return client; }
private void build() throws IOException { if (init) { return; } ssh = new SSHClient(); ssh.addHostKeyVerifier(new HostKeyVerifier() { @Override public boolean verify(String arg0, int arg1, PublicKey arg2) { return true; } }); ssh.connect(hostname, port); if (privateKey != null) { privateKeyFile = File.createTempFile("zstack", "tmp"); FileUtils.writeStringToFile(privateKeyFile, privateKey); ssh.authPublickey(username, privateKeyFile.getAbsolutePath()); } else { ssh.authPassword(username, password); } init = true; }
SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); ssh.connect("nameOfServer"); ssh.authPublickey("userId"); Session session = ssh.startSession(); Command cmd = session.exec("yourCommand"); System.out.println(cmd.getOutputAsString()); session.close(); ssh.disconnect();
public void connect() throws IOException { ssh.connect(host); if (connectMethod.equals(ConnectMethod.PASSWORD)) { ssh.authPassword(user, password); } else { ssh.authPublickey(user, keyFile); } }
public static void main(String[] args) throws IOException { final SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); ssh.connect("10.x.x.x"); try { //ssh.authPublickey(System.getProperty("root")); ssh.authPassword("user", "xxxx"); final Session session = ssh.startSession(); try { final Command cmd = session.exec("cd /backup; ls; ./backup.sh"); System.out.println(IOUtils.readFully(cmd.getInputStream()).toString()); cmd.join(5, TimeUnit.SECONDS); System.out.println("\n** exit status: " + cmd.getExitStatus()); } finally { session.close(); } } finally { ssh.disconnect(); } }
SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); ssh.connect("your-server"); ssh.authPublickey("userId"); //START SESSION Session session = ssh.startSession(); //SEND THE COMMAND Command cmd = session.exec("shutdown"); //GET THE OUTPUT OF COMMAND cmd.getOutputAsString(); //CLOSE SESSION session.close(); ssh.disconnect();
@Override public final void connectWithPubKeyAuth(final String keyfilePath) throws RaspiQueryException { LOGGER.info("Connecting to host '{}' on port '{}'.", hostname, port); client = newAndroidSSHClient(); LOGGER.info("Using no host key verification."); client.addHostKeyVerifier(new PromiscuousVerifier()); try { client.connect(hostname, port); LOGGER.info("Using private/public key authentication, keyfile: '{}'", keyfilePath); KeyProvider keyProvider = client.loadKeys(keyfilePath); client.authPublickey(username, keyProvider); } catch (UserAuthException e) { LOGGER.info("Authentication failed.", e); throw RaspiQueryException.createAuthenticationFailure(hostname, username, e); } catch (TransportException e) { throw RaspiQueryException.createTransportFailure(hostname, e); } catch (IOException e) { throw RaspiQueryException .createConnectionFailure(hostname, port, e); } }
final SSHClient ssh = new SSHClient(); ssh.addHostKeyVerifier("XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX"); ssh.connect("host"); PKCS8KeyFile keyFile = new PKCS8KeyFile(); keyFile.init(new File("server_pem.pem")); ssh.auth("ec2-user", new AuthPublickey(keyFile)); try { final Session session = ssh.startSession(); try { final Command command = session.exec("whoami"); String response = IOUtils.readFully(command.getInputStream()).toString(); command.join(10, TimeUnit.SECONDS); return response; } finally { session.close(); } } finally { ssh.disconnect(); }
@Override public final void connect(String password) throws RaspiQueryException { LOGGER.info("Connecting to host '{}' on port '{}'.", hostname, port); client = newAndroidSSHClient(); LOGGER.info("Using no host key verification."); client.addHostKeyVerifier(new PromiscuousVerifier()); try { client.connect(hostname, port); client.authPassword(username, password); } catch (UserAuthException e) { throw RaspiQueryException.createAuthenticationFailure(hostname, username, e); } catch (TransportException e) { throw RaspiQueryException.createTransportFailure(hostname, e); } catch (IOException e) { throw RaspiQueryException.createConnectionFailure(hostname, port, e); } }
final SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); // or, to skip host verification: ssh.addHostKeyVerifier(new PromiscuousVerifier()) ssh.connect("localhost"); try { ssh.authPassword("user", "password"); // or ssh.authPublickey(System.getProperty("user.name")) final SFTPClient sftp = ssh.newSFTPClient(); try { sftp.get("test_file", "/tmp/test.tmp"); } finally { sftp.close(); } } finally { ssh.disconnect(); }
@Override public SSHClient create() throws Exception { ssh = new net.schmizz.sshj.SSHClient(); ssh.addHostKeyVerifier(new PromiscuousVerifier()); if (connectTimeout != 0) { ssh.setConnectTimeout(connectTimeout); } if (sessionTimeout != 0) { ssh.setTimeout(sessionTimeout); } ssh.connect(hostAndPort.getHostText(), hostAndPort.getPortOrDefault(22)); if (loginCredentials.hasUnencryptedPrivateKey()) { OpenSSHKeyFile key = new OpenSSHKeyFile(); key.init(loginCredentials.getOptionalPrivateKey().get(), null); ssh.authPublickey(loginCredentials.getUser(), key); } else if (loginCredentials.getOptionalPassword().isPresent()) { ssh.authPassword(loginCredentials.getUser(), loginCredentials.getOptionalPassword().get()); } else if (agentConnector.isPresent()) { AgentProxy proxy = new AgentProxy(agentConnector.get()); ssh.auth(loginCredentials.getUser(), getAuthMethods(proxy)); } return ssh; }
public void sshExec(String cmd) throws IOException { @SuppressWarnings("resource") final SSHClient ssh = new SSHClient(); ssh.addHostKeyVerifier(new PromiscuousVerifier()); try { ssh.connect("localhost", vm.getSshForwardPort()); ssh.authPassword("root", ""); try (Session session = ssh.startSession()) { final Session.Command sessionCmd = session.exec(cmd); sessionCmd.join(5, TimeUnit.SECONDS); int exitCode = sessionCmd.getExitStatus(); if (exitCode != 0) { throw new RuntimeException("command " + cmd + " returned exit status code " + exitCode); } } } finally { if (ssh.isConnected()) { ssh.disconnect(); } } }
/** * Create a new {@code SSHClient} connected to the remote machine using the * AdminAccess credentials as provided */ public static SSHClient newClient( Machine machine, AdminAccess adminAccess, int timeoutInMillis ) throws IOException { checkArgument(timeoutInMillis >= 0, "timeoutInMillis should be positive or 0"); final SSHClient client = new SSHClient(); client.addHostKeyVerifier(AcceptAnyHostKeyVerifier.INSTANCE); if (timeoutInMillis != 0) { client.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT); client.setTimeout(timeoutInMillis); } client.connect(machine.getPublicDnsName(), machine.getSshPort()); OpenSSHKeyFile key = new OpenSSHKeyFile(); key.init(adminAccess.getPrivateKey(), adminAccess.getPublicKey()); client.authPublickey(adminAccess.getUsername(), key); return client; }
private Session getSession(InvocationContext context) throws IOException { try { /* * if it is connected, create a session Note: one client can have multiple session (on one channel) */ if (ssh.isConnected()) return ssh.startSession(); if (sshContext == null) { sshContext = ((SSHSecurityContextImpl) context.getSecurityContext(SSH_SECURITY_CONTEXT)); } KeyProvider pkey = ssh.loadKeys(sshContext.getPrivateKeyLoc(), sshContext.getKeyPass()); ssh.loadKnownHosts(); ssh.authPublickey(sshContext.getUsername(), pkey); ssh.connect(context.getExecutionDescription().getHost().getType().getHostAddress()); return ssh.startSession(); } catch (NullPointerException ne) { throw new SecurityException("Cannot load security context for SSH", ne); } }
@SuppressWarnings("resource") private SSHResources tryGetSSHSession() throws IOException { final SSHClient ssh = new SSHClient(); Session session = null; try { ssh.addHostKeyVerifier(new PromiscuousVerifier()); ssh.connect("localhost", vm.getSshForwardPort()); ssh.authPassword("root", ""); session = ssh.startSession(); return new SSHResources(ssh, session); } catch (Throwable ex) { if (session != null) { try { session.close(); } catch (Throwable ex1) { } } if (ssh.isConnected()) { ssh.disconnect(); } throw ex; } }
public Session getSession(String hostAddress) throws IOException { try { if (sshClient == null) { sshClient = new SSHClient(); } if (getSSHClient().isConnected()) return getSSHClient().startSession(); KeyProvider pkey = getSSHClient().loadKeys(getPrivateKeyLoc(), getKeyPass()); getSSHClient().loadKnownHosts(); getSSHClient().connect(hostAddress); getSSHClient().authPublickey(getUsername(), pkey); session = getSSHClient().startSession(); return session; } catch (NullPointerException ne) { throw new SecurityException("Cannot load security context for SSH", ne); } }
@Override public SSHClient create() throws Exception { ssh = new net.schmizz.sshj.SSHClient(); ssh.addHostKeyVerifier(new PromiscuousVerifier()); if (connectTimeout != 0) { ssh.setConnectTimeout(connectTimeout); } if (sessionTimeout != 0) { ssh.setTimeout(sessionTimeout); } ssh.connect(hostAndPort.getHostText(), hostAndPort.getPortOrDefault(22)); if (loginCredentials.getPassword() != null) { ssh.authPassword(loginCredentials.getUser(), loginCredentials.getPassword()); } else { OpenSSHKeyFile key = new OpenSSHKeyFile(); key.init(loginCredentials.getPrivateKey(), null); ssh.authPublickey(loginCredentials.getUser(), key); } return ssh; }
@Override public SSHClient create() throws Exception { ssh = new net.schmizz.sshj.SSHClient(); ssh.addHostKeyVerifier(new PromiscuousVerifier()); if (connectTimeout != 0) { ssh.setConnectTimeout(connectTimeout); } if (sessionTimeout != 0) { ssh.setTimeout(sessionTimeout); } ssh.connect(hostAndPort.getHostText(), hostAndPort.getPortOrDefault(22)); if (loginCredentials.getPassword() != null) { ssh.authPassword(loginCredentials.getUser(), loginCredentials.getPassword()); } else { OpenSSHKeyFile key = new OpenSSHKeyFile(); key.init(loginCredentials.getPrivateKey(), null); ssh.authPublickey(loginCredentials.getUser(), key); } return ssh; }
@Override public SSHClient create() throws Exception { ssh = new net.schmizz.sshj.SSHClient(); ssh.addHostKeyVerifier(new PromiscuousVerifier()); if (connectTimeout != 0) { ssh.setConnectTimeout(connectTimeout); } if (sessionTimeout != 0) { ssh.setTimeout(sessionTimeout); } ssh.connect(hostAndPort.getHostText(), hostAndPort.getPortOrDefault(22)); if (loginCredentials.getPassword() != null) { ssh.authPassword(loginCredentials.getUser(), loginCredentials.getPassword()); } else { OpenSSHKeyFile key = new OpenSSHKeyFile(); key.init(loginCredentials.getPrivateKey(), null); ssh.authPublickey(loginCredentials.getUser(), key); } return ssh; }