Search in sources :

Example 6 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 7 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 8 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)

Example 9 with Connection

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

the class ConfigTest method executeTest.

public boolean executeTest() {
    int error = 0;
    Element rootElement = this.getInputFile().get(0).getDocumentElement();
    NodeList commandLst = rootElement.getElementsByTagName("command");
    //Analyze each command, send request and build the array list of api commands
    for (int i = 0; i < commandLst.getLength(); i++) {
        Node fstNode = commandLst.item(i);
        Element fstElmnt = (Element) fstNode;
        //new command
        ApiCommand api = new ApiCommand(fstElmnt, this.getParam(), this.getCommands());
        if (api.getName().equals("rebootManagementServer")) {
            s_logger.info("Attempting to SSH into management server " + this.getParam().get("hostip"));
            try {
                Connection conn = new Connection(this.getParam().get("hostip"));
                conn.connect(null, 60000, 60000);
                s_logger.info("SSHed successfully into management server " + this.getParam().get("hostip"));
                boolean isAuthenticated = conn.authenticateWithPassword("root", "password");
                if (isAuthenticated == false) {
                    s_logger.info("Authentication failed for root with password");
                    return false;
                }
                String restartCommand = "service cloud-management restart; service cloud-usage restart";
                Session sess = conn.openSession();
                s_logger.info("Executing : " + restartCommand);
                sess.execCommand(restartCommand);
                Thread.sleep(120000);
                sess.close();
                conn.close();
            } catch (Exception ex) {
                s_logger.error(ex);
                return false;
            }
        } else {
            //send a command
            api.sendCommand(this.getClient(), null);
            //verify the response of the command
            if ((api.getResponseType() == ResponseType.ERROR) && (api.getResponseCode() == 200) && (api.getTestCaseInfo() != null)) {
                s_logger.error("Test case " + api.getTestCaseInfo() + "failed. Command that was supposed to fail, passed. The command was sent with the following url " + api.getUrl());
                error++;
            } else if ((api.getResponseType() != ResponseType.ERROR) && (api.getResponseCode() == 200)) {
                //set parameters for the future use
                if (api.setParam(this.getParam()) == false) {
                    s_logger.error("Exiting the test...Command " + api.getName() + " didn't return parameters needed for the future use. The command was sent with url " + api.getUrl());
                    return false;
                } else {
                    //verify parameters
                    if (api.verifyParam() == false) {
                        s_logger.error("Command " + api.getName() + " failed. Verification for returned parameters failed. Command was sent with url " + api.getUrl());
                        error++;
                    } else if (api.getTestCaseInfo() != null) {
                        s_logger.info("Test case " + api.getTestCaseInfo() + " passed. Command was sent with the url " + api.getUrl());
                    }
                }
            } else if ((api.getResponseType() != ResponseType.ERROR) && (api.getResponseCode() != 200)) {
                s_logger.error("Command " + api.getName() + " failed with an error code " + api.getResponseCode() + " . Command was sent with url  " + api.getUrl() + " Required: " + api.getRequired());
                if (api.getRequired() == true) {
                    s_logger.info("The command is required for the future use, so exiging");
                    return false;
                }
                error++;
            } else if (api.getTestCaseInfo() != null) {
                s_logger.info("Test case " + api.getTestCaseInfo() + " passed. Command that was supposed to fail, failed - test passed. Command was sent with url " + api.getUrl());
            }
        }
    }
    if (error != 0)
        return false;
    else
        return true;
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 10 with Connection

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

the class EventsApiTest method executeTest.

public boolean executeTest() {
    int error = 0;
    Element rootElement = this.getInputFile().get(0).getDocumentElement();
    NodeList commandLst = rootElement.getElementsByTagName("command");
    //Analyze each command, send request and build the array list of api commands
    for (int i = 0; i < commandLst.getLength(); i++) {
        Node fstNode = commandLst.item(i);
        Element fstElmnt = (Element) fstNode;
        //!!!check if we need to execute mySql command
        NodeList commandName = fstElmnt.getElementsByTagName("name");
        Element commandElmnt = (Element) commandName.item(0);
        NodeList commandNm = commandElmnt.getChildNodes();
        if (((Node) commandNm.item(0)).getNodeValue().equals("mysqlupdate")) {
            //establish connection to mysql server and execute an update command
            NodeList mysqlList = fstElmnt.getElementsByTagName("mysqlcommand");
            for (int j = 0; j < mysqlList.getLength(); j++) {
                Element itemVariableElement = (Element) mysqlList.item(j);
                s_logger.info("Executing mysql command " + itemVariableElement.getTextContent());
                try {
                    Statement st = this.getConn().createStatement();
                    st.executeUpdate(itemVariableElement.getTextContent());
                } catch (Exception ex) {
                    s_logger.error(ex);
                    return false;
                }
            }
        } else if (((Node) commandNm.item(0)).getNodeValue().equals("agentcommand")) {
            //connect to all the agents and execute agent command
            NodeList commandList = fstElmnt.getElementsByTagName("commandname");
            Element commandElement = (Element) commandList.item(0);
            NodeList ipList = fstElmnt.getElementsByTagName("ip");
            for (int j = 0; j < ipList.getLength(); j++) {
                Element itemVariableElement = (Element) ipList.item(j);
                s_logger.info("Attempting to SSH into agent " + itemVariableElement.getTextContent());
                try {
                    Connection conn = new Connection(itemVariableElement.getTextContent());
                    conn.connect(null, 60000, 60000);
                    s_logger.info("SSHed successfully into agent " + itemVariableElement.getTextContent());
                    boolean isAuthenticated = conn.authenticateWithPassword("root", "password");
                    if (isAuthenticated == false) {
                        s_logger.info("Authentication failed for root with password");
                        return false;
                    }
                    Session sess = conn.openSession();
                    s_logger.info("Executing : " + commandElement.getTextContent());
                    sess.execCommand(commandElement.getTextContent());
                    Thread.sleep(60000);
                    sess.close();
                    conn.close();
                } catch (Exception ex) {
                    s_logger.error(ex);
                    return false;
                }
            }
        } else {
            //new command
            ApiCommand api = new ApiCommand(fstElmnt, this.getParam(), this.getCommands());
            //send a command
            api.sendCommand(this.getClient(), null);
            //verify the response of the command
            if ((api.getResponseType() == ResponseType.ERROR) && (api.getResponseCode() == 200)) {
                s_logger.error("Test case " + api.getTestCaseInfo() + " failed. Command that was supposed to fail, passed. The command was sent with the following url " + api.getUrl());
                error++;
            } else if ((api.getResponseType() != ResponseType.ERROR) && (api.getResponseCode() == 200)) {
                //verify if response is suppposed to be empty
                if (api.getResponseType() == ResponseType.EMPTY) {
                    if (api.isEmpty() == true) {
                        s_logger.info("Test case " + api.getTestCaseInfo() + " passed. Empty response was returned as expected. Command was sent with url " + api.getUrl());
                    } else {
                        s_logger.error("Test case " + api.getTestCaseInfo() + " failed. Empty response was expected. Command was sent with url " + api.getUrl());
                    }
                } else {
                    if (api.isEmpty() != false)
                        s_logger.error("Test case " + api.getTestCaseInfo() + " failed. Non-empty response was expected. Command was sent with url " + api.getUrl());
                    else {
                        //set parameters for the future use
                        if (api.setParam(this.getParam()) == false) {
                            s_logger.error("Exiting the test...Command " + api.getName() + " didn't return parameters needed for the future use. The command was sent with url " + api.getUrl());
                            return false;
                        } else if (api.getTestCaseInfo() != null) {
                            s_logger.info("Test case " + api.getTestCaseInfo() + " passed. Command was sent with the url " + api.getUrl());
                        }
                    }
                }
            } else if ((api.getResponseType() != ResponseType.ERROR) && (api.getResponseCode() != 200)) {
                s_logger.error("Command " + api.getName() + " failed with an error code " + api.getResponseCode() + " . Command was sent with url  " + api.getUrl());
                if (api.getRequired() == true) {
                    s_logger.info("The command is required for the future use, so exiging");
                    return false;
                }
                error++;
            } else if (api.getTestCaseInfo() != null) {
                s_logger.info("Test case " + api.getTestCaseInfo() + " passed. Command that was supposed to fail, failed. Command was sent with url " + api.getUrl());
            }
        }
    }
    //verify events with userid parameter - test case 97
    HashMap<String, Integer> expectedEvents = new HashMap<String, Integer>();
    expectedEvents.put("VM.START", 1);
    boolean eventResult = ApiCommand.verifyEvents(expectedEvents, "INFO", "http://" + this.getParam().get("hostip") + ":8096", "userid=" + this.getParam().get("userid1") + "&type=VM.START");
    s_logger.info("Test case 97 - listEvent command verification result is  " + eventResult);
    //verify error events
    eventResult = ApiCommand.verifyEvents("../metadata/error_events.properties", "ERROR", "http://" + this.getParam().get("hostip") + ":8096", this.getParam().get("erroruseraccount"));
    s_logger.info("listEvent command verification result is  " + eventResult);
    if (error != 0)
        return false;
    else
        return true;
}
Also used : HashMap(java.util.HashMap) Statement(java.sql.Statement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Aggregations

Connection (com.trilead.ssh2.Connection)40 IOException (java.io.IOException)37 Session (com.trilead.ssh2.Session)36 InputStream (java.io.InputStream)23 SCPClient (com.trilead.ssh2.SCPClient)16 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 HttpException (org.apache.commons.httpclient.HttpException)8 File (java.io.File)7 Connection (okhttp3.Connection)6 Request (okhttp3.Request)6 Connection (org.jboss.remoting3.Connection)6 StreamGobbler (com.trilead.ssh2.StreamGobbler)5 ConfigurationException (javax.naming.ConfigurationException)5 MediaType (okhttp3.MediaType)5 RequestBody (okhttp3.RequestBody)5 Response (okhttp3.Response)5 ResponseBody (okhttp3.ResponseBody)5 Connection (ch.ethz.ssh2.Connection)4 Charset (java.nio.charset.Charset)4