Search in sources :

Example 56 with Connection

use of org.jboss.remoting3.Connection in project warn-report by saaavsaaa.

the class LinuxExecUtil method connect.

public Connection connect(final String host, final int port, final String user, final String password) throws IOException {
    conn = new Connection(host, port);
    conn.connect();
    boolean isAuthenticated = conn.authenticateWithPassword(user, password);
    if (isAuthenticated == false) {
        throw new IOException("Authentication failed.");
    }
    return conn;
}
Also used : Connection(ch.ethz.ssh2.Connection)

Example 57 with Connection

use of org.jboss.remoting3.Connection in project Payara by payara.

the class SSHLauncher method openConnection.

/**
 * Opens the connection to the host and authenticates with public
 * key.
 */
private void openConnection() throws IOException {
    boolean isAuthenticated = false;
    String message = "";
    connection = new Connection(host, port);
    connection.connect(new HostVerifier(knownHostsDatabase));
    if (SSHUtil.checkString(keyFile) == null && SSHUtil.checkString(password) == null && privateKey == null) {
        message += "No key or password specified - trying default keys \n";
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("keyfile and password are null. Will try to authenticate with default key file if available");
        }
        // check the default key locations if no authentication
        // method is explicitly configured.
        File home = new File(System.getProperty("user.home"));
        for (String keyName : Arrays.asList("id_rsa", "id_dsa", "identity")) {
            message += "Tried to authenticate using " + keyName + "\n";
            File key = new File(home, ".ssh/" + keyName);
            if (key.exists()) {
                isAuthenticated = connection.authenticateWithPublicKey(userName, key, null);
            }
            if (isAuthenticated) {
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Authentication successful using key " + keyName);
                }
                message = null;
                break;
            }
        }
    }
    if (!isAuthenticated && SSHUtil.checkString(password) != null) {
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("Authenticating with password " + getPrintablePassword(password));
        }
        try {
            isAuthenticated = connection.authenticateWithPassword(userName, password);
        } catch (IOException iex) {
            message = "SSH authentication with password failed: " + ExceptionUtil.getRootCause(iex).getMessage();
            logger.log(Level.WARNING, message, iex);
        }
    }
    if (!isAuthenticated && privateKey != null) {
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("Authenticating with privateKey");
        }
        try {
            isAuthenticated = connection.authenticateWithPublicKey(userName, privateKey, keyPassPhrase);
        } catch (IOException iex) {
            message = "SSH authentication with private key failed: " + ExceptionUtil.getRootCause(iex).getMessage();
            logger.log(Level.WARNING, message, iex);
        }
    }
    if (!isAuthenticated && SSHUtil.checkString(keyFile) != null) {
        if (logger.isLoggable(Level.FINER)) {
            logger.finer("Specified key file is " + keyFile);
        }
        File key = new File(keyFile);
        if (key.exists()) {
            if (logger.isLoggable(Level.FINER)) {
                logger.finer("Specified key file exists at " + key);
            }
            try {
                isAuthenticated = connection.authenticateWithPublicKey(userName, key, keyPassPhrase);
            } catch (IOException iex) {
                message = "SSH authentication with key file " + key + " failed: " + ExceptionUtil.getRootCause(iex).getMessage();
                logger.log(Level.WARNING, message, iex);
            }
        }
    }
    if (!isAuthenticated && !connection.isAuthenticationComplete()) {
        connection.close();
        connection = null;
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("Could not authenticate");
        }
        throw new IOException("Could not authenticate. " + message);
    }
    message = null;
    SSHUtil.register(connection);
}
Also used : HostVerifier(org.glassfish.cluster.ssh.util.HostVerifier) Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException) File(java.io.File)

Aggregations

Connection (com.trilead.ssh2.Connection)40 Session (com.trilead.ssh2.Session)31 IOException (java.io.IOException)28 InputStream (java.io.InputStream)21 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 HttpException (org.apache.commons.httpclient.HttpException)8 SCPClient (com.trilead.ssh2.SCPClient)7 Connection (org.jboss.remoting3.Connection)6 StreamGobbler (com.trilead.ssh2.StreamGobbler)5 Connection (okhttp3.Connection)5 Request (okhttp3.Request)5 Connection (ch.ethz.ssh2.Connection)4 File (java.io.File)4 Principal (java.security.Principal)4 MediaType (okhttp3.MediaType)4 RequestBody (okhttp3.RequestBody)4 Response (okhttp3.Response)4 ResponseBody (okhttp3.ResponseBody)4 SecurityIdentity (org.wildfly.security.auth.server.SecurityIdentity)4