Search in sources :

Example 96 with Session

use of com.google.spanner.v1.Session in project cloudstack by apache.

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 97 with Session

use of com.google.spanner.v1.Session in project cloudstack by apache.

the class GuestNetwork method run.

@Override
public void run() {
    NDC.push("Following thread has started" + Thread.currentThread().getName());
    int retry = 0;
    // Start copying files between machines in the network
    s_logger.info("The size of the array is " + this.virtualMachines.size());
    while (true) {
        try {
            if (retry > 0) {
                s_logger.info("Retry attempt : " + retry + " ...sleeping 120 seconds before next attempt");
                Thread.sleep(120000);
            }
            for (VirtualMachine vm : this.virtualMachines) {
                s_logger.info("Attempting to SSH into linux host " + this.publicIp + " with retry attempt: " + retry);
                Connection conn = new Connection(this.publicIp);
                conn.connect(null, 600000, 600000);
                s_logger.info("SSHed successfully into linux host " + this.publicIp);
                boolean isAuthenticated = conn.authenticateWithPassword("root", "password");
                if (isAuthenticated == false) {
                    s_logger.info("Authentication failed");
                }
                // execute copy command
                Session sess = conn.openSession();
                String fileName;
                Random ran = new Random();
                fileName = Math.abs(ran.nextInt()) + "-file";
                String copyCommand = new String("./scpScript " + vm.getPrivateIp() + " " + fileName);
                s_logger.info("Executing " + copyCommand);
                sess.execCommand(copyCommand);
                Thread.sleep(120000);
                sess.close();
                // execute wget command
                sess = conn.openSession();
                String downloadCommand = new String("wget http://172.16.0.220/scripts/checkDiskSpace.sh; chmod +x *sh; ./checkDiskSpace.sh; rm -rf checkDiskSpace.sh");
                s_logger.info("Executing " + downloadCommand);
                sess.execCommand(downloadCommand);
                Thread.sleep(120000);
                sess.close();
                // close the connection
                conn.close();
            }
        } catch (Exception ex) {
            s_logger.error(ex);
            retry++;
            if (retry == retryNum) {
                s_logger.info("Performance Guest Network test failed with error " + ex.getMessage());
            }
        }
    }
}
Also used : Random(java.util.Random) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 98 with Session

use of com.google.spanner.v1.Session in project cloudstack by apache.

the class EventsApiTest method executeTest.

@Override
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 (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 (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)

Example 99 with Session

use of com.google.spanner.v1.Session in project cloudstack by apache.

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 100 with Session

use of com.google.spanner.v1.Session in project warn-report by saaavsaaa.

the class LinuxExecUtil method execute.

public void execute(final String commandText) {
    try {
        System.out.println("exec : " + commandText);
        Session sess = conn.openSession();
        sess.execCommand(commandText);
        System.out.println("Here is some information about the remote host:");
        InputStream standardOut = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(standardOut));
        while (true) {
            String line = br.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
        }
        System.out.println("ExitCode: " + sess.getExitStatus());
        sess.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : StreamGobbler(ch.ethz.ssh2.StreamGobbler) Session(ch.ethz.ssh2.Session)

Aggregations

Session (com.trilead.ssh2.Session)43 Session (org.neo4j.driver.v1.Session)38 Connection (com.trilead.ssh2.Connection)32 IOException (java.io.IOException)30 Test (org.junit.Test)29 InputStream (java.io.InputStream)28 Driver (org.neo4j.driver.v1.Driver)27 StatementResult (org.neo4j.driver.v1.StatementResult)20 Record (org.neo4j.driver.v1.Record)12 Session (iaik.pkcs.pkcs11.Session)10 TokenException (iaik.pkcs.pkcs11.TokenException)10 P11TokenException (org.xipki.security.exception.P11TokenException)10 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)9 RoutingNetworkSession (org.neo4j.driver.internal.RoutingNetworkSession)9 Session (ch.ethz.ssh2.Session)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 HttpException (org.apache.commons.httpclient.HttpException)8 Transaction (org.neo4j.driver.v1.Transaction)7 SCPClient (com.trilead.ssh2.SCPClient)6