/** * Authenticate {@code username} using the {@code "publickey"} authentication method, with keys from some common * locations on the file system. This method relies on {@code ~/.ssh/id_rsa} and {@code ~/.ssh/id_dsa}. * <p/> * This method does not provide a way to specify a passphrase. * * @param username user to authenticate * * @throws UserAuthException in case of authentication failure * @throws TransportException if there was a transport-layer error */ public void authPublickey(String username) throws UserAuthException, TransportException { final String base = System.getProperty("user.home") + File.separator + ".ssh" + File.separator; authPublickey(username, base + "id_rsa", base + "id_dsa", base + "id_ed25519", base + "id_ecdsa"); }
/** * Authenticate {@code username} using the {@code "publickey"} authentication method. * <p/> * {@link KeyProvider} instances can be created using any of the {@code loadKeys()} method provided in this class. * In case multiple {@code keyProviders} are specified; authentication is attempted in order as long as the {@code * "publickey"} authentication method is available. * * @param username user to authenticate * @param keyProviders one or more {@link KeyProvider} instances * * @throws UserAuthException in case of authentication failure * @throws TransportException if there was a transport-layer error */ public void authPublickey(String username, KeyProvider... keyProviders) throws UserAuthException, TransportException { authPublickey(username, Arrays.<KeyProvider>asList(keyProviders)); }
/** * Authenticate {@code username} using the {@code "publickey"} authentication method, with keys from one or more * {@code locations} in the file system. * <p/> * In case multiple {@code locations} are specified; authentication is attempted in order as long as the {@code * "publickey"} authentication method is available. If there is an error loading keys from any of them (e.g. file * could not be read, file format not recognized) that key file it is ignored. * <p/> * This method does not provide a way to specify a passphrase. * * @param username user to authenticate * @param locations one or more locations in the file system containing the private key * * @throws UserAuthException in case of authentication failure * @throws TransportException if there was a transport-layer error */ public void authPublickey(String username, String... locations) throws UserAuthException, TransportException { final List<KeyProvider> keyProviders = new LinkedList<KeyProvider>(); for (String loc : locations) { try { log.debug("Attempting to load key from: {}", loc); keyProviders.add(loadKeys(loc)); } catch (IOException logged) { log.info("Could not load keys from {} due to: {}", loc, logged.getMessage()); } } authPublickey(username, keyProviders); }
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(); }
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();
/** * Authenticate {@code username} using the {@code "publickey"} authentication method, with keys from some common * locations on the file system. This method relies on {@code ~/.ssh/id_rsa} and {@code ~/.ssh/id_dsa}. * <p/> * This method does not provide a way to specify a passphrase. * * @param username user to authenticate * * @throws UserAuthException in case of authentication failure * @throws TransportException if there was a transport-layer error */ public void authPublickey(String username) throws UserAuthException, TransportException { final String base = System.getProperty("user.home") + File.separator + ".ssh" + File.separator; authPublickey(username, base + "id_rsa", base + "id_dsa", base + "id_ed25519", base + "id_ecdsa"); }
/** * Authenticate {@code username} using the {@code "publickey"} authentication method, with keys from some common * locations on the file system. This method relies on {@code ~/.ssh/id_rsa} and {@code ~/.ssh/id_dsa}. * <p/> * This method does not provide a way to specify a passphrase. * * @param username user to authenticate * * @throws UserAuthException in case of authentication failure * @throws TransportException if there was a transport-layer error */ public void authPublickey(String username) throws UserAuthException, TransportException { final String base = System.getProperty("user.home") + File.separator + ".ssh" + File.separator; authPublickey(username, base + "id_rsa", base + "id_dsa"); }
/** * Authenticate {@code username} using the {@code "publickey"} authentication method. * <p/> * {@link KeyProvider} instances can be created using any of the {@code loadKeys()} method provided in this class. * In case multiple {@code keyProviders} are specified; authentication is attempted in order as long as the {@code * "publickey"} authentication method is available. * * @param username user to authenticate * @param keyProviders one or more {@link KeyProvider} instances * * @throws UserAuthException in case of authentication failure * @throws TransportException if there was a transport-layer error */ public void authPublickey(String username, KeyProvider... keyProviders) throws UserAuthException, TransportException { authPublickey(username, Arrays.<KeyProvider>asList(keyProviders)); }
/** * Authenticate {@code username} using the {@code "publickey"} authentication method. * <p/> * {@link KeyProvider} instances can be created using any of the {@code loadKeys()} method provided in this class. * In case multiple {@code keyProviders} are specified; authentication is attempted in order as long as the {@code * "publickey"} authentication method is available. * * @param username user to authenticate * @param keyProviders one or more {@link KeyProvider} instances * * @throws UserAuthException in case of authentication failure * @throws TransportException if there was a transport-layer error */ public void authPublickey(String username, KeyProvider... keyProviders) throws UserAuthException, TransportException { authPublickey(username, Arrays.<KeyProvider>asList(keyProviders)); }
public void connect() throws IOException { ssh.connect(host); if (connectMethod.equals(ConnectMethod.PASSWORD)) { ssh.authPassword(user, password); } else { ssh.authPublickey(user, keyFile); } }
@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); } }
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 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; }
/** * 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); } }
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; }