Search in sources :

Example 1 with Connection

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

the class StressTestDirectAttach method sshTest.

private static String sshTest(String host, String password) {
    int i = 0;
    if (host == null) {
        s_logger.info("Did not receive a host back from test, ignoring ssh test");
        return null;
    }
    if (password == null) {
        s_logger.info("Did not receive a password back from test, ignoring ssh test");
        return null;
    }
    // We will retry 5 times before quitting
    String result = null;
    int retry = 0;
    while (true) {
        try {
            if (retry > 0) {
                s_logger.info("Retry attempt : " + retry + " ...sleeping 120 seconds before next attempt. Account is " + _account.get());
                Thread.sleep(120000);
            }
            s_logger.info("Attempting to SSH into linux host " + host + " with retry attempt: " + retry + ". Account is " + _account.get());
            Connection conn = new Connection(host);
            conn.connect(null, 60000, 60000);
            s_logger.info("User + " + _account.get() + " ssHed successfully into linux host " + host);
            boolean isAuthenticated = conn.authenticateWithPassword("root", password);
            if (isAuthenticated == false) {
                s_logger.info("Authentication failed for root with password" + password);
                return "Authentication failed";
            }
            boolean success = false;
            String linuxCommand = null;
            if (i % 10 == 0)
                linuxCommand = "rm -rf *; wget http://192.168.1.250/dump.bin && ls -al dump.bin";
            else
                linuxCommand = "wget http://192.168.1.250/dump.bin && ls -al dump.bin";
            Session sess = conn.openSession();
            s_logger.info("User " + _account.get() + " executing : " + linuxCommand);
            sess.execCommand(linuxCommand);
            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) {
                retry++;
                if (retry == MAX_RETRY_LINUX) {
                    result = "SSH Linux Network test fail";
                }
            }
            return result;
        } catch (Exception e) {
            retry++;
            s_logger.error("SSH Linux Network test fail with error");
            if (retry == MAX_RETRY_LINUX) {
                return "SSH Linux Network test fail with error " + e.getMessage();
            }
        }
        i++;
    }
}
Also used : 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 2 with Connection

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

the class TestClient method sshTest.

private static String sshTest(String host) {
    if (host == null) {
        s_logger.info("Did not receive a host back from test, ignoring 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 120 seconds before next attempt");
                Thread.sleep(120000);
            }
            s_logger.info("Attempting to SSH into linux host " + host + " with retry attempt: " + retry);
            Connection conn = new Connection(host);
            conn.connect(null, 60000, 60000);
            s_logger.info("SSHed successfully into linux host " + host);
            boolean isAuthenticated = conn.authenticateWithPassword("root", "password");
            if (isAuthenticated == false) {
                return "Authentication failed";
            }
            boolean success = false;
            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 && ls -al 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_LINUX) {
                    return "SSH Linux Network test fail";
                }
            }
        } catch (Exception e) {
            retry++;
            if (retry == MAX_RETRY_LINUX) {
                return "SSH Linux Network test fail with error " + e.getMessage();
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 3 with Connection

use of com.trilead.ssh2.Connection 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 4 with Connection

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

the class WgetTest method main.

public static void main(String[] args) {
    // Parameters
    List<String> argsList = Arrays.asList(args);
    Iterator<String> iter = argsList.iterator();
    while (iter.hasNext()) {
        String arg = iter.next();
        // host
        if (arg.equals("-h")) {
            host = iter.next();
        }
        if (arg.equals("-p")) {
            password = iter.next();
        }
    }
    int i = 0;
    if (host == null || host.equals("")) {
        s_logger.info("Did not receive a host back from test, ignoring ssh test");
        System.exit(2);
    }
    if (password == null) {
        s_logger.info("Did not receive a password back from test, ignoring ssh test");
        System.exit(2);
    }
    int retry = 0;
    try {
        if (retry > 0) {
            s_logger.info("Retry attempt : " + retry + " ...sleeping 120 seconds before next attempt");
            Thread.sleep(120000);
        }
        s_logger.info("Attempting to SSH into linux host " + host + " with retry attempt: " + retry);
        Connection conn = new Connection(host);
        conn.connect(null, 60000, 60000);
        s_logger.info("User + ssHed successfully into linux host " + host);
        boolean isAuthenticated = conn.authenticateWithPassword("root", password);
        if (isAuthenticated == false) {
            s_logger.info("Authentication failed for root with password" + password);
            System.exit(2);
        }
        boolean success = false;
        String linuxCommand = null;
        if (i % 10 == 0)
            linuxCommand = "rm -rf *; wget http://192.168.1.250/dump.bin && ls -al dump.bin";
        else
            linuxCommand = "wget http://192.168.1.250/dump.bin && ls -al dump.bin";
        Session sess = conn.openSession();
        sess.execCommand(linuxCommand);
        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.");
                    System.exit(2);
                }
                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) {
            retry++;
            if (retry == MAX_RETRY_LINUX) {
                System.exit(2);
            }
        }
    } catch (Exception e) {
        retry++;
        s_logger.error("SSH Linux Network test fail with error");
        if (retry == MAX_RETRY_LINUX) {
            s_logger.error("Ssh test failed");
            System.exit(2);
        }
    }
}
Also used : InputStream(java.io.InputStream) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 5 with Connection

use of com.trilead.ssh2.Connection 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)

Aggregations

Connection (org.ovirt.engine.sdk4.Connection)63 Connection (com.trilead.ssh2.Connection)54 IOException (java.io.IOException)52 Session (com.trilead.ssh2.Session)39 VmsService (org.ovirt.engine.sdk4.services.VmsService)30 Vm (org.ovirt.engine.sdk4.types.Vm)30 InputStream (java.io.InputStream)28 SCPClient (com.trilead.ssh2.SCPClient)20 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)18 VmService (org.ovirt.engine.sdk4.services.VmService)18 File (java.io.File)13 SystemService (org.ovirt.engine.sdk4.services.SystemService)13 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)12 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)12 Connection (okhttp3.Connection)11 Request (okhttp3.Request)10 Response (okhttp3.Response)9 Connection (ch.ethz.ssh2.Connection)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 ConfigurationException (javax.naming.ConfigurationException)8