Search in sources :

Example 16 with SCPClient

use of ch.ethz.ssh2.SCPClient in project Payara by payara.

the class LogFilterForInstance method downloadAllInstanceLogFiles.

public void downloadAllInstanceLogFiles(ServiceLocator habitat, Server targetServer, Domain domain, Logger logger, String instanceName, String tempDirectoryOnServer, String instanceLogFileDirectory) throws IOException {
    // method is used from collect-log-files command
    // for Instance it's going through this loop. This will use ssh utility to get file from instance machine(remote machine) and
    // store in  tempDirectoryOnServer which is used to create zip file.
    // Right now user needs to go through this URL to setup and configure ssh http://wikis.sun.com/display/GlassFish/3.1SSHSetup
    SSHLauncher sshL = getSSHL(habitat);
    String sNode = targetServer.getNodeRef();
    Nodes nodes = domain.getNodes();
    Node node = nodes.getNode(sNode);
    if (node.getType().equals("SSH")) {
        sshL.init(node, logger);
        List<String> allInstanceLogFileName = getInstanceLogFileNames(habitat, targetServer, domain, logger, instanceName, instanceLogFileDirectory);
        boolean noFileFound = true;
        String sourceDir = getLoggingDirectoryForNode(instanceLogFileDirectory, node, sNode, instanceName);
        SFTPClient sftpClient = sshL.getSFTPClient();
        try {
            List instanceLogFileNames = sftpClient.ls(sourceDir);
            for (int i = 0; i < instanceLogFileNames.size(); i++) {
                SFTPv3DirectoryEntry file = (SFTPv3DirectoryEntry) instanceLogFileNames.get(i);
                String fileName = file.filename;
                // code to remove . and .. file which is return from sftpclient ls method
                if (!file.attributes.isDirectory() && !fileName.equals(".") && !fileName.equals("..") && fileName.contains(".log") && !fileName.contains(".log.")) {
                    noFileFound = false;
                    break;
                }
            }
        } catch (Exception e) {
            // if directory doesn't present or missing on remote machine. It happens due to bug 16451
            noFileFound = true;
        }
        if (noFileFound) {
            // this loop is used when user has changed value for server.log but not restarted the server.
            sourceDir = getLoggingDirectoryForNodeWhenNoFilesFound(instanceLogFileDirectory, node, sNode, instanceName);
        }
        String[] remoteFileNames = new String[allInstanceLogFileName.size()];
        for (int i = 0; i < allInstanceLogFileName.size(); i++) {
            remoteFileNames[i] = sourceDir + File.separator + allInstanceLogFileName.get(i);
        }
        sftpClient.close();
        SCPClient scpClient = sshL.getSCPClient();
        scpClient.get(remoteFileNames, tempDirectoryOnServer);
    } else if (node.getType().equals("DCOM")) {
        List instanceLogFileNames = getInstanceLogFileNames(habitat, targetServer, domain, logger, instanceName, instanceLogFileDirectory);
        String sourceDir = getLoggingDirectoryForNode(instanceLogFileDirectory, node, sNode, instanceName);
        try {
            DcomInfo info = new DcomInfo(node);
            WindowsRemoteFileSystem wrfs = new WindowsRemoteFileSystem(info.getHost(), info.getUser(), info.getPassword());
            for (int i = 0; i < instanceLogFileNames.size(); i++) {
                String logFileName = (String) instanceLogFileNames.get(i);
                WindowsRemoteFile wrf = new WindowsRemoteFile(wrfs, sourceDir + File.separator + logFileName);
                File instanceLogFile = new File(tempDirectoryOnServer + File.separator + logFileName);
                wrf.copyTo(instanceLogFile);
            }
        } catch (WindowsException ex) {
            throw new IOException("Unable to download instance log file from DCOM Instance Node");
        }
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) SSHLauncher(org.glassfish.cluster.ssh.launcher.SSHLauncher) Node(com.sun.enterprise.config.serverbeans.Node) SFTPClient(org.glassfish.cluster.ssh.sftp.SFTPClient) IOException(java.io.IOException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) Nodes(com.sun.enterprise.config.serverbeans.Nodes) IOException(java.io.IOException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) DcomInfo(org.glassfish.cluster.ssh.util.DcomInfo) SFTPv3DirectoryEntry(com.trilead.ssh2.SFTPv3DirectoryEntry) WindowsRemoteFile(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile) WindowsRemoteFileSystem(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFileSystem) ArrayList(java.util.ArrayList) List(java.util.List) WindowsRemoteFile(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile) File(java.io.File)

Example 17 with SCPClient

use of ch.ethz.ssh2.SCPClient in project warn-report by saaavsaaa.

the class LinuxExecUtil method upload.

public void upload(final String localFile, final String remoteTargetDirectory) throws IOException {
    SCPClient clt = conn.createSCPClient();
    clt.put(localFile, remoteTargetDirectory);
}
Also used : SCPClient(ch.ethz.ssh2.SCPClient)

Example 18 with SCPClient

use of ch.ethz.ssh2.SCPClient in project cosmic by MissionCriticalCloud.

the class CitrixResourceBase method copyConfigDriveIsoToHost.

public boolean copyConfigDriveIsoToHost(final Connection conn, final SR sr, final String vmName) {
    final String vmIso = "/tmp/" + vmName + "/configDrive/" + vmName + ".iso";
    // scp file into the host
    final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_host.getIp(), 22);
    try {
        sshConnection.connect(null, 60000, 60000);
        if (!sshConnection.authenticateWithPassword(_username, _password.peek())) {
            throw new CloudRuntimeException("Unable to authenticate");
        }
        s_logger.debug("scp config drive iso file " + vmIso + " to host " + _host.getIp() + " path " + _configDriveIsopath);
        final SCPClient scp = new SCPClient(sshConnection);
        final String p = "0755";
        scp.put(vmIso, _configDriveIsopath, p);
        sr.scan(conn);
        s_logger.debug("copied config drive iso to host " + _host);
    } catch (final IOException e) {
        s_logger.debug("failed to copy configdrive iso " + vmIso + " to host " + _host, e);
        return false;
    } catch (final XmlRpcException e) {
        s_logger.debug("Failed to scan config drive iso SR " + _configDriveSRName + _host.getIp() + " in host " + _host, e);
        return false;
    } finally {
        sshConnection.close();
        // clean up the config drive files
        final String configDir = "/tmp/" + vmName;
        try {
            deleteLocalFolder(configDir);
            s_logger.debug("Successfully cleaned up config drive directory " + configDir + " after copying it to host ");
        } catch (final Exception e) {
            s_logger.debug("Failed to delete config drive folder :" + configDir + " for VM " + vmName + " " + e.getMessage());
        }
    }
    return true;
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) IOException(java.io.IOException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 19 with SCPClient

use of ch.ethz.ssh2.SCPClient in project cosmic by MissionCriticalCloud.

the class ScpTemplateDownloader method download.

@Override
public long download(final boolean resume, final DownloadCompleteCallback callback) {
    if (_status == Status.ABORTED || _status == Status.UNRECOVERABLE_ERROR || _status == Status.DOWNLOAD_FINISHED) {
        return 0;
    }
    _resume = resume;
    _start = System.currentTimeMillis();
    final URI uri;
    try {
        uri = new URI(_downloadUrl);
    } catch (final URISyntaxException e1) {
        _status = Status.UNRECOVERABLE_ERROR;
        return 0;
    }
    final String username = uri.getUserInfo();
    final String queries = uri.getQuery();
    String password = null;
    if (queries != null) {
        final String[] qs = queries.split("&");
        for (final String q : qs) {
            final String[] tokens = q.split("=");
            if (tokens[0].equalsIgnoreCase("password")) {
                password = tokens[1];
                break;
            }
        }
    }
    int port = uri.getPort();
    if (port == -1) {
        port = 22;
    }
    final File file = new File(_toFile);
    final 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");
        }
        final SCPClient scp = new SCPClient(sshConnection);
        final 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();
        final String downloaded = "(download complete)";
        _errorString = "Downloaded " + _remoteSize + " bytes " + downloaded;
        _downloadTime += System.currentTimeMillis() - _start;
        return _totalBytes;
    } catch (final 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 20 with SCPClient

use of ch.ethz.ssh2.SCPClient in project nimbus by nimbus-org.

the class SCPClientImpl method connect.

public void connect(String user, String host, int port, String password) throws SCPException {
    if (connection != null) {
        throw new SCPException("It is already connected!");
    }
    connection = new Connection(host, port);
    try {
        if (isTcpNoDelay != null) {
            connection.setTCPNoDelay(isTcpNoDelay.booleanValue());
        }
        if (serverHostKeyAlgorithms != null) {
            connection.setServerHostKeyAlgorithms(serverHostKeyAlgorithms);
        }
        connection.connect(null, connectionTimeout, keyExchangeTimeout);
        if (!connection.authenticateWithPassword(user, password)) {
            throw new SCPException("It failed to authenticate!");
        }
        scpClient = connection.createSCPClient();
    } catch (IOException e) {
        scpClient = null;
        connection.close();
        connection = null;
        throw new SCPException("It failed to connect!", e);
    }
}
Also used : SCPException(jp.ossc.nimbus.service.scp.SCPException) Connection(ch.ethz.ssh2.Connection) IOException(java.io.IOException)

Aggregations

SCPClient (com.trilead.ssh2.SCPClient)21 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)13 IOException (java.io.IOException)12 ConfigurationException (javax.naming.ConfigurationException)7 Connection (com.trilead.ssh2.Connection)6 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 Connection (ch.ethz.ssh2.Connection)2 SCPClient (ch.ethz.ssh2.SCPClient)2 Host (com.xensource.xenapi.Host)2 XenAPIObject (com.xensource.xenapi.XenAPIObject)2 MalformedURLException (java.net.MalformedURLException)2