Search in sources :

Example 1 with SCPClient

use of com.trilead.ssh2.SCPClient in project CloudStack-archive by CloudStack-extras.

the class TestClient method sshWinTest.

private static String sshWinTest(String host) {
    if (host == null) {
        s_logger.info("Did not receive a host back from test, ignoring win ssh test");
        return null;
    }
    // We will retry 5 times before quitting
    int retry = 0;
    while (true) {
        try {
            if (retry > 0) {
                s_logger.info("Retry attempt : " + retry + " ...sleeping 300 seconds before next attempt");
                Thread.sleep(300000);
            }
            s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry);
            Connection conn = new Connection(host);
            conn.connect(null, 60000, 60000);
            s_logger.info("SSHed successfully into windows host " + host);
            boolean success = false;
            boolean isAuthenticated = conn.authenticateWithPassword("vmops", "vmops");
            if (isAuthenticated == false) {
                return "Authentication failed";
            }
            SCPClient scp = new SCPClient(conn);
            scp.put("wget.exe", "");
            Session sess = conn.openSession();
            s_logger.info("Executing : wget http://172.16.0.220/dump.bin");
            sess.execCommand("wget http://172.16.0.220/dump.bin && dir dump.bin");
            InputStream stdout = sess.getStdout();
            InputStream stderr = sess.getStderr();
            byte[] buffer = new byte[8192];
            while (true) {
                if ((stdout.available() == 0) && (stderr.available() == 0)) {
                    int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
                    if ((conditions & ChannelCondition.TIMEOUT) != 0) {
                        s_logger.info("Timeout while waiting for data from peer.");
                        return null;
                    }
                    if ((conditions & ChannelCondition.EOF) != 0) {
                        if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
                            break;
                        }
                    }
                }
                while (stdout.available() > 0) {
                    success = true;
                    int len = stdout.read(buffer);
                    if (// this check is somewhat paranoid
                    len > 0)
                        s_logger.info(new String(buffer, 0, len));
                }
                while (stderr.available() > 0) {
                    int len = stderr.read(buffer);
                }
            }
            sess.close();
            conn.close();
            if (success) {
                return null;
            } else {
                retry++;
                if (retry == MAX_RETRY_WIN) {
                    return "SSH Windows Network test fail";
                }
            }
        } catch (Exception e) {
            retry++;
            if (retry == MAX_RETRY_WIN) {
                return "SSH Windows Network test fail with error " + e.getMessage();
            }
        }
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 2 with SCPClient

use of com.trilead.ssh2.SCPClient in project CloudStack-archive by CloudStack-extras.

the class StressTestDirectAttach method sshWinTest.

private static String sshWinTest(String host) {
    if (host == null) {
        s_logger.info("Did not receive a host back from test, ignoring win ssh test");
        return null;
    }
    // We will retry 5 times before quitting
    int retry = 1;
    while (true) {
        try {
            if (retry > 0) {
                s_logger.info("Retry attempt : " + retry + " ...sleeping 300 seconds before next attempt. Account is " + _account.get());
                Thread.sleep(300000);
            }
            s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry + " for account " + _account.get());
            Connection conn = new Connection(host);
            conn.connect(null, 60000, 60000);
            s_logger.info("User " + _account.get() + " ssHed successfully into windows host " + host);
            boolean success = false;
            boolean isAuthenticated = conn.authenticateWithPassword("Administrator", "password");
            if (isAuthenticated == false) {
                return "Authentication failed";
            } else {
                s_logger.info("Authentication is successfull");
            }
            try {
                SCPClient scp = new SCPClient(conn);
                scp.put("wget.exe", "wget.exe", "C:\\Users\\Administrator", "0777");
                s_logger.info("Successfully put wget.exe file");
            } catch (Exception ex) {
                s_logger.error("Unable to put wget.exe " + ex);
            }
            if (conn == null) {
                s_logger.error("Connection is null");
            }
            Session sess = conn.openSession();
            s_logger.info("User + " + _account.get() + " executing : wget http://192.168.1.250/dump.bin");
            sess.execCommand("wget http://192.168.1.250/dump.bin && dir dump.bin");
            InputStream stdout = sess.getStdout();
            InputStream stderr = sess.getStderr();
            byte[] buffer = new byte[8192];
            while (true) {
                if ((stdout.available() == 0) && (stderr.available() == 0)) {
                    int conditions = sess.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 120000);
                    if ((conditions & ChannelCondition.TIMEOUT) != 0) {
                        s_logger.info("Timeout while waiting for data from peer.");
                        return null;
                    }
                    if ((conditions & ChannelCondition.EOF) != 0) {
                        if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
                            break;
                        }
                    }
                }
                while (stdout.available() > 0) {
                    success = true;
                    int len = stdout.read(buffer);
                    if (// this check is somewhat paranoid
                    len > 0)
                        s_logger.info(new String(buffer, 0, len));
                }
                while (stderr.available() > 0) {
                    /* int len = */
                    stderr.read(buffer);
                }
            }
            sess.close();
            conn.close();
            if (success) {
                Thread.sleep(120000);
                return null;
            } else {
                retry++;
                if (retry == MAX_RETRY_WIN) {
                    return "SSH Windows Network test fail for account " + _account.get();
                }
            }
        } catch (Exception e) {
            s_logger.error(e);
            retry++;
            if (retry == MAX_RETRY_WIN) {
                return "SSH Windows Network test fail with error " + e.getMessage();
            }
        }
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Session(com.trilead.ssh2.Session)

Example 3 with SCPClient

use of com.trilead.ssh2.SCPClient in project acceptance-test-harness by jenkinsci.

the class Ssh method copyTo.

public void copyTo(String localFile, String remoteFile, String targetDir) throws IOException {
    SCPClient scpClient = new SCPClient(connection);
    scpClient.put(localFile, remoteFile, targetDir, "0755");
}
Also used : SCPClient(com.trilead.ssh2.SCPClient)

Example 4 with SCPClient

use of com.trilead.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 5 with SCPClient

use of com.trilead.ssh2.SCPClient in project Payara by payara.

the class InstallNodeSshCommand method copyToHostsInternal.

private void copyToHostsInternal(File zipFile, ArrayList<String> binDirFiles) throws IOException, InterruptedException, CommandException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    boolean prompt = promptPass;
    for (String host : hosts) {
        sshLauncher.init(getRemoteUser(), host, getRemotePort(), sshpassword, getSshKeyFile(), sshkeypassphrase, logger);
        if (getSshKeyFile() != null && !sshLauncher.checkConnection()) {
            // key auth failed, so use password auth
            prompt = true;
        }
        if (prompt) {
            String sshpass = null;
            if (sshPasswords.containsKey(host))
                sshpass = String.valueOf(sshPasswords.get(host));
            else
                sshpass = getSSHPassword(host);
            // re-initialize
            sshLauncher.init(getRemoteUser(), host, getRemotePort(), sshpass, getSshKeyFile(), sshkeypassphrase, logger);
            prompt = false;
        }
        String sshInstallDir = getInstallDir().replace('\\', '/');
        SFTPClient sftpClient = sshLauncher.getSFTPClient();
        SCPClient scpClient = sshLauncher.getSCPClient();
        try {
            if (!sftpClient.exists(sshInstallDir)) {
                sftpClient.mkdirs(sshInstallDir, 0755);
            }
        } catch (IOException ioe) {
            logger.info(Strings.get("mkdir.failed", sshInstallDir, host));
            throw new IOException(ioe);
        }
        // delete the sshInstallDir contents if non-empty
        try {
            // get list of file in DAS sshInstallDir
            List<String> files = getListOfInstallFiles(sshInstallDir);
            deleteRemoteFiles(sftpClient, files, sshInstallDir, getForce());
        } catch (IOException ex) {
            logger.finer("Failed to remove sshInstallDir contents");
            throw new IOException(ex);
        }
        String zip = zipFile.getCanonicalPath();
        try {
            logger.info("Copying " + zip + " (" + zipFile.length() + " bytes)" + " to " + host + ":" + sshInstallDir);
            // Looks like we need to quote the paths to scp in case they
            // contain spaces.
            scpClient.put(zipFile.getAbsolutePath(), FileUtils.quoteString(sshInstallDir));
            if (logger.isLoggable(Level.FINER))
                logger.finer("Copied " + zip + " to " + host + ":" + sshInstallDir);
        } catch (IOException ex) {
            logger.info(Strings.get("cannot.copy.zip.file", zip, host));
            throw new IOException(ex);
        }
        try {
            logger.info("Installing " + getArchiveName() + " into " + host + ":" + sshInstallDir);
            String unzipCommand = "cd '" + sshInstallDir + "'; jar -xvf " + getArchiveName();
            int status = sshLauncher.runCommand(unzipCommand, outStream);
            if (status != 0) {
                logger.info(Strings.get("jar.failed", host, outStream.toString()));
                throw new CommandException("Remote command output: " + outStream.toString());
            }
            if (logger.isLoggable(Level.FINER))
                logger.finer("Installed " + getArchiveName() + " into " + host + ":" + sshInstallDir);
        } catch (IOException ioe) {
            logger.info(Strings.get("jar.failed", host, outStream.toString()));
            throw new IOException(ioe);
        }
        try {
            logger.info("Removing " + host + ":" + sshInstallDir + "/" + getArchiveName());
            sftpClient.rm(sshInstallDir + "/" + getArchiveName());
            if (logger.isLoggable(Level.FINER))
                logger.finer("Removed " + host + ":" + sshInstallDir + "/" + getArchiveName());
        } catch (IOException ioe) {
            logger.info(Strings.get("remove.glassfish.failed", host, sshInstallDir));
            throw new IOException(ioe);
        }
        // unjarring doesn't retain file permissions, hence executables need
        // to be fixed with proper permissions
        logger.info("Fixing file permissions of all bin files under " + host + ":" + sshInstallDir);
        try {
            if (binDirFiles.isEmpty()) {
                // binDirFiles can be empty if the archive isn't a fresh one
                searchAndFixBinDirectoryFiles(sshInstallDir, sftpClient);
            } else {
                for (String binDirFile : binDirFiles) {
                    sftpClient.chmod((sshInstallDir + "/" + binDirFile), 0755);
                }
            }
            if (logger.isLoggable(Level.FINER))
                logger.finer("Fixed file permissions of all bin files " + "under " + host + ":" + sshInstallDir);
        } catch (IOException ioe) {
            logger.info(Strings.get("fix.permissions.failed", host, sshInstallDir));
            throw new IOException(ioe);
        }
        if (Constants.v4) {
            logger.info("Fixing file permissions for nadmin file under " + host + ":" + sshInstallDir + "/" + SystemPropertyConstants.getComponentName() + "/lib");
            try {
                sftpClient.chmod((sshInstallDir + "/" + SystemPropertyConstants.getComponentName() + "/lib/nadmin"), 0755);
                if (logger.isLoggable(Level.FINER))
                    logger.finer("Fixed file permission for nadmin under " + host + ":" + sshInstallDir + "/" + SystemPropertyConstants.getComponentName() + "/lib/nadmin");
            } catch (IOException ioe) {
                logger.info(Strings.get("fix.permissions.failed", host, sshInstallDir));
                throw new IOException(ioe);
            }
        }
        sftpClient.close();
    }
}
Also used : SCPClient(com.trilead.ssh2.SCPClient) SFTPClient(org.glassfish.cluster.ssh.sftp.SFTPClient) CommandException(org.glassfish.api.admin.CommandException)

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