Search in sources :

Example 11 with SCPClient

use of com.trilead.ssh2.SCPClient in project cloudstack by apache.

the class SshHelper method scpFrom.

public static void scpFrom(String host, int port, String user, File permKeyFile, String localTargetDirectory, String remoteTargetFile) throws Exception {
    com.trilead.ssh2.Connection conn = null;
    com.trilead.ssh2.SCPClient scpClient = null;
    try {
        conn = new com.trilead.ssh2.Connection(host, port);
        conn.connect(null, DEFAULT_CONNECT_TIMEOUT, DEFAULT_KEX_TIMEOUT);
        if (!conn.authenticateWithPublicKey(user, permKeyFile, null)) {
            String msg = "Failed to authentication SSH user " + user + " on host " + host;
            s_logger.error(msg);
            throw new Exception(msg);
        }
        scpClient = conn.createSCPClient();
        scpClient.get(remoteTargetFile, localTargetDirectory);
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException)

Example 12 with SCPClient

use of com.trilead.ssh2.SCPClient in project cloudstack by apache.

the class SshHelper method scpTo.

public static void scpTo(String host, int port, String user, File pemKeyFile, String password, String remoteTargetDirectory, String localFile, String fileMode, int connectTimeoutInMs, int kexTimeoutInMs) throws Exception {
    com.trilead.ssh2.Connection conn = null;
    com.trilead.ssh2.SCPClient scpClient = null;
    try {
        conn = new com.trilead.ssh2.Connection(host, port);
        conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
        if (pemKeyFile == null) {
            if (!conn.authenticateWithPassword(user, password)) {
                String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        } else {
            if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
                String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        scpClient = conn.createSCPClient();
        if (fileMode != null)
            scpClient.put(localFile, remoteTargetDirectory, fileMode);
        else
            scpClient.put(localFile, remoteTargetDirectory);
    } finally {
        if (conn != null)
            conn.close();
    }
}
Also used : Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException)

Example 13 with SCPClient

use of com.trilead.ssh2.SCPClient in project cloudstack by apache.

the class SshHelper method scpTo.

public static void scpTo(String host, int port, String user, File pemKeyFile, String password, String remoteTargetDirectory, byte[] data, String remoteFileName, String fileMode, int connectTimeoutInMs, int kexTimeoutInMs) throws Exception {
    com.trilead.ssh2.Connection conn = null;
    com.trilead.ssh2.SCPClient scpClient = null;
    try {
        conn = new com.trilead.ssh2.Connection(host, port);
        conn.connect(null, connectTimeoutInMs, kexTimeoutInMs);
        if (pemKeyFile == null) {
            if (!conn.authenticateWithPassword(user, password)) {
                String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        } else {
            if (!conn.authenticateWithPublicKey(user, pemKeyFile, password)) {
                String msg = "Failed to authentication SSH user " + user + " on host " + host;
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        scpClient = conn.createSCPClient();
        if (fileMode != null)
            scpClient.put(data, remoteFileName, remoteTargetDirectory, fileMode);
        else
            scpClient.put(data, remoteFileName, remoteTargetDirectory);
    } finally {
        if (conn != null)
            conn.close();
    }
}
Also used : Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException)

Example 14 with SCPClient

use of com.trilead.ssh2.SCPClient in project cloudstack by apache.

the class ScpTemplateDownloader method download.

@Override
public long download(boolean resume, DownloadCompleteCallback callback) {
    if (_status == Status.ABORTED || _status == Status.UNRECOVERABLE_ERROR || _status == Status.DOWNLOAD_FINISHED) {
        return 0;
    }
    _resume = resume;
    _start = System.currentTimeMillis();
    URI uri;
    try {
        uri = new URI(_downloadUrl);
    } catch (URISyntaxException e1) {
        _status = Status.UNRECOVERABLE_ERROR;
        return 0;
    }
    String username = uri.getUserInfo();
    String queries = uri.getQuery();
    String password = null;
    if (queries != null) {
        String[] qs = queries.split("&");
        for (String q : qs) {
            String[] tokens = q.split("=");
            if (tokens[0].equalsIgnoreCase("password")) {
                password = tokens[1];
                break;
            }
        }
    }
    int port = uri.getPort();
    if (port == -1) {
        port = 22;
    }
    File file = new File(_toFile);
    com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(uri.getHost(), port);
    try {
        if (_storage != null) {
            file.createNewFile();
            _storage.setWorldReadableAndWriteable(file);
        }
        sshConnection.connect(null, 60000, 60000);
        if (!sshConnection.authenticateWithPassword(username, password)) {
            throw new CloudRuntimeException("Unable to authenticate");
        }
        SCPClient scp = new SCPClient(sshConnection);
        String src = uri.getPath();
        _status = Status.IN_PROGRESS;
        scp.get(src, _toDir);
        if (!file.exists()) {
            _status = Status.UNRECOVERABLE_ERROR;
            s_logger.debug("unable to scp the file " + _downloadUrl);
            return 0;
        }
        _status = Status.DOWNLOAD_FINISHED;
        _totalBytes = file.length();
        String downloaded = "(download complete)";
        _errorString = "Downloaded " + _remoteSize + " bytes " + downloaded;
        _downloadTime += System.currentTimeMillis() - _start;
        return _totalBytes;
    } catch (Exception e) {
        s_logger.warn("Unable to download " + _downloadUrl, e);
        _status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
        _errorString = e.getMessage();
        return 0;
    } finally {
        sshConnection.close();
        if (_status == Status.UNRECOVERABLE_ERROR && file.exists()) {
            file.delete();
        }
        if (callback != null) {
            callback.downloadComplete(_status);
        }
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) URISyntaxException(java.net.URISyntaxException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) File(java.io.File)

Example 15 with SCPClient

use of com.trilead.ssh2.SCPClient in project cloudstack by apache.

the class Ovm3HypervisorSupport method setupServer.

/**
 * setupServer:
 * Add the cloudstack plugin and setup the agent.
 * Add the ssh keys to the host.
 *
 * @param c
 * @throws IOException
 */
public Boolean setupServer(String key) throws IOException {
    LOGGER.debug("Setup all bits on agent: " + config.getAgentHostname());
    /* version dependent patching ? */
    try {
        com.trilead.ssh2.Connection sshConnection = SSHCmdHelper.acquireAuthorizedConnection(config.getAgentIp(), config.getAgentSshUserName(), config.getAgentSshPassword());
        if (sshConnection == null) {
            throw new ConfigurationException(String.format("Unable to " + "connect to server(IP=%1$s, username=%2$s, " + "password=%3$s", config.getAgentIp(), config.getAgentSshUserName(), config.getAgentSshPassword()));
        }
        SCPClient scp = new SCPClient(sshConnection);
        String userDataScriptDir = "scripts/vm/hypervisor/ovm3/";
        String userDataScriptPath = Script.findScript("", userDataScriptDir);
        if (userDataScriptPath == null) {
            throw new ConfigurationException("Can not find " + userDataScriptDir);
        }
        String mkdir = "mkdir -p " + config.getAgentScriptsDir();
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, mkdir)) {
            throw new ConfigurationException("Failed " + mkdir + " on " + config.getAgentHostname());
        }
        for (String script : config.getAgentScripts()) {
            script = userDataScriptPath + "/" + script;
            scp.put(script, config.getAgentScriptsDir(), "0755");
        }
        String prepareCmd = String.format(config.getAgentScriptsDir() + "/" + config.getAgentScript() + " --ssl=" + c.getUseSsl() + " " + "--port=" + c.getPort());
        if (!SSHCmdHelper.sshExecuteCmd(sshConnection, prepareCmd)) {
            throw new ConfigurationException("Failed to insert module on " + config.getAgentHostname());
        } else {
            /* because of OVM 3.3.1 (might be 2000) */
            Thread.sleep(5000);
        }
        CloudstackPlugin cSp = new CloudstackPlugin(c);
        cSp.ovsUploadSshKey(config.getAgentSshKeyFileName(), FileUtils.readFileToString(getSystemVMKeyFile(key)));
        cSp.dom0CheckStorageHealthCheck(config.getAgentScriptsDir(), config.getAgentCheckStorageScript(), config.getCsHostGuid(), config.getAgentStorageCheckTimeout(), config.getAgentStorageCheckInterval());
    } catch (Exception es) {
        LOGGER.error("Unexpected exception ", es);
        String msg = "Unable to install module in agent";
        throw new CloudRuntimeException(msg);
    }
    return true;
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CloudstackPlugin(com.cloud.hypervisor.ovm3.objects.CloudstackPlugin) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) Ovm3ResourceException(com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException)

Aggregations

SCPClient (com.trilead.ssh2.SCPClient)22 IOException (java.io.IOException)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)13 Connection (com.trilead.ssh2.Connection)12 ConfigurationException (javax.naming.ConfigurationException)7 Session (com.trilead.ssh2.Session)6 File (java.io.File)6 InputStream (java.io.InputStream)6 Connection (com.xensource.xenapi.Connection)4 XenAPIException (com.xensource.xenapi.Types.XenAPIException)4 URISyntaxException (java.net.URISyntaxException)4 URLConnection (java.net.URLConnection)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HttpException (org.apache.commons.httpclient.HttpException)4 XmlRpcException (org.apache.xmlrpc.XmlRpcException)4 SFTPClient (org.glassfish.cluster.ssh.sftp.SFTPClient)3 Host (com.xensource.xenapi.Host)2 XenAPIObject (com.xensource.xenapi.XenAPIObject)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2