/** * Adds a {@link OpenSSHKnownHosts} object created from the specified location as a host key verifier. * * @param location location for {@code known_hosts} file * * @throws IOException if there is an error loading from any of these locations */ public void loadKnownHosts(File location) throws IOException { addHostKeyVerifier(new OpenSSHKnownHosts(location, loggerFactory)); }
/** * Add a {@link HostKeyVerifier} that will verify any host that's able to claim a host key with the given {@code * fingerprint}. * * The fingerprint can be specified in either an MD5 colon-delimited format (16 hexadecimal octets, delimited by a colon), * or in a Base64 encoded format for SHA-1 or SHA-256 fingerprints. * Valid examples are: * * <ul><li>"SHA1:2Fo8c/96zv32xc8GZWbOGYOlRak="</li> * <li>"SHA256:oQGbQTujGeNIgh0ONthcEpA/BHxtt3rcYY+NxXTxQjs="</li> * <li>"MD5:d3:5e:40:72:db:08:f1:6d:0c:d7:6d:35:0d:ba:7c:32"</li> * <li>"d3:5e:40:72:db:08:f1:6d:0c:d7:6d:35:0d:ba:7c:32"</li></ul> * * @param fingerprint expected fingerprint in colon-delimited format (16 octets in hex delimited by a colon) * * @see SecurityUtils#getFingerprint */ public void addHostKeyVerifier(final String fingerprint) { addHostKeyVerifier(FingerprintVerifier.getInstance(fingerprint)); }
public SSHClient setupClient(Config config) { if (client == null) { client = new SSHClient(config); client.addHostKeyVerifier(fingerprint); } 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; }
/** * Add a {@link HostKeyVerifier} that will verify any host that's able to claim a host key with the given {@code * fingerprint}, e.g. {@code "4b:69:6c:72:6f:79:20:77:61:73:20:68:65:72:65:21"} * * @param fingerprint expected fingerprint in colon-delimited format (16 octets in hex delimited by a colon) * * @see SecurityUtils#getFingerprint */ public void addHostKeyVerifier(final String fingerprint) { addHostKeyVerifier(new HostKeyVerifier() { @Override public boolean verify(String h, int p, PublicKey k) { return SecurityUtils.getFingerprint(k).equals(fingerprint); } }); }
/** * Adds a {@link OpenSSHKnownHosts} object created from the specified location as a host key verifier. * * @param location location for {@code known_hosts} file * * @throws IOException if there is an error loading from any of these locations */ public void loadKnownHosts(File location) throws IOException { addHostKeyVerifier(new OpenSSHKnownHosts(location, loggerFactory)); }
/** * Adds a {@link OpenSSHKnownHosts} object created from the specified location as a host key verifier. * * @param location location for {@code known_hosts} file * * @throws IOException if there is an error loading from any of these locations */ public void loadKnownHosts(File location) throws IOException { addHostKeyVerifier(new OpenSSHKnownHosts(location)); }
... final SSHClient ssh = new SSHClient(); ssh.addHostKeyVerifier(new NullHostKeyVerifier()); ...
/** * Add a {@link HostKeyVerifier} that will verify any host that's able to claim a host key with the given {@code * fingerprint}. * * The fingerprint can be specified in either an MD5 colon-delimited format (16 hexadecimal octets, delimited by a colon), * or in a Base64 encoded format for SHA-1 or SHA-256 fingerprints. * Valid examples are: * * <ul><li>"SHA1:2Fo8c/96zv32xc8GZWbOGYOlRak="</li> * <li>"SHA256:oQGbQTujGeNIgh0ONthcEpA/BHxtt3rcYY+NxXTxQjs="</li> * <li>"MD5:d3:5e:40:72:db:08:f1:6d:0c:d7:6d:35:0d:ba:7c:32"</li> * <li>"d3:5e:40:72:db:08:f1:6d:0c:d7:6d:35:0d:ba:7c:32"</li></ul> * * @param fingerprint expected fingerprint in colon-delimited format (16 octets in hex delimited by a colon) * * @see SecurityUtils#getFingerprint */ public void addHostKeyVerifier(final String fingerprint) { addHostKeyVerifier(FingerprintVerifier.getInstance(fingerprint)); }
private void configureConnection(String serverFingerPrint, Boolean checkIdentity) throws IOException { createKnownHosts(); ssh = new SSHClient(); ssh.loadKnownHosts(); if (!checkIdentity) { ssh.addHostKeyVerifier(new PromiscuousVerifier()); } else if (serverFingerPrint != null) { ssh.addHostKeyVerifier(serverFingerPrint); } else { File khFile = new File(OpenSSHKnownHosts.detectSSHDir(), "known_hosts"); ssh.addHostKeyVerifier(new OpenSSHKnownHosts(khFile)); } }
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); } }
@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; }
@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; }
@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; } }
@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; }