Package vicazh.hyperpool.stream.net.pop3

Examples of vicazh.hyperpool.stream.net.pop3.Connection


                    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 (len > 0) // this check is somewhat paranoid
                            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++;
View Full Code Here


                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;
                    }
View Full Code Here

            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;
                }
View Full Code Here

                    Thread.sleep(300000);
                }

                s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry + " for account " + s_account.get());

                Connection conn = new Connection(host);
                conn.connect(null, 60000, 60000);

                s_logger.info("User " + s_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 + " + s_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 (len > 0) // this check is somewhat paranoid
                            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 {
View Full Code Here

                    Thread.sleep(120000);
                }

                s_logger.info("Attempting to SSH into linux host " + host + " with retry attempt: " + retry + ". Account is " + s_account.get());

                Connection conn = new Connection(host);
                conn.connect(null, 60000, 60000);

                s_logger.info("User + " + s_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 " + s_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 (len > 0) // this check is somewhat paranoid
                            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";
View Full Code Here

                    Thread.sleep(300000);
                }

                s_logger.info("Attempting to SSH into windows host " + host + " with retry attempt: " + retry + " for account " + s_account.get());

                Connection conn = new Connection(host);
                conn.connect(null, 60000, 60000);

                s_logger.info("User " + s_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 + " + s_account.get() + " executing : wget http://" + downloadUrl);
                String downloadCommand = "wget http://" + downloadUrl + " && dir dump.bin";
                sess.execCommand(downloadCommand);

                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 (len > 0) // this check is somewhat paranoid
                            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++;
View Full Code Here

                    Thread.sleep(120000);
                }

                s_logger.info("Attempting to SSH into linux host " + host + " with retry attempt: " + retry + ". Account is " + s_account.get());

                Connection conn = new Connection(host);
                conn.connect(null, 60000, 60000);

                s_logger.info("User + " + s_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://" + downloadUrl + " && ls -al dump.bin";
                else
                    linuxCommand = "wget http://" + downloadUrl + " && ls -al dump.bin";

                Session sess = conn.openSession();
                s_logger.info("User " + s_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 (len > 0) // this check is somewhat paranoid
                            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";
View Full Code Here

     */
    private void openConnection() throws IOException {

    boolean isAuthenticated = false;
    String message= "";
    connection = new Connection(host, port);

        connection.connect(new HostVerifier(knownHostsDatabase));
        if(SSHUtil.checkString(keyFile) == null && SSHUtil.checkString(password) == null) {
            message += "No key or password specified - trying default keys \n";
            if (logger.isLoggable(Level.FINE)) {
View Full Code Here

        //password is must for key distribution
        if (passwd == null) {
            throw new IOException("SSH password is required for distributing the public key. You can specify the SSH password in a password file and pass it through --passwordfile option.");
        }
        connection = new Connection(node, port);
        connection.connect();
        connected = connection.authenticateWithPassword(userName, passwd);

        if(!connected) {
            throw new IOException("SSH password authentication failed for user " + userName + " on host " + node);
        }
       
        //We open up a second connection for scp and exec. For some reason, a hang
        //is seen in MKS if we try to do everything using the same connection.
        Connection conn = new Connection(node, port);
        conn.connect();
        boolean ret = conn.authenticateWithPassword(userName, passwd);
       
        if (!ret) {
            throw new IOException("SSH password authentication failed for user " + userName + " on host " + node);
        }
        //initiate scp client
        SCPClient scp = new SCPClient(conn);
        SFTPClient sftp = new SFTPClient(connection);

        if (key.exists()) {

            //fixes .ssh file mode
            setupSSHDir();

            if (pubKeyFile == null) {
                pubKeyFile = keyFile + ".pub";
            }

            File pubKey = new File(pubKeyFile);
            if(!pubKey.exists()) {
                throw new IOException("Public key file " + pubKeyFile + " does not exist.");
            }

            try {
                if(!sftp.exists(SSH_DIR)) {
                    if(logger.isLoggable(Level.FINER)) {
                        logger.fine(SSH_DIR + " does not exist");
                    }
                    sftp.mkdirs(".ssh", 0700);
                }
            } catch (Exception e) {
                if(logger.isLoggable(Level.FINER)) {
                    e.printStackTrace();
                }
                throw new IOException("Error while creating .ssh directory on remote host:" + e.getMessage());
            }

            //copy over the public key to remote host
            scp.put(pubKey.getAbsolutePath(), "key.tmp", ".ssh", "0600");           

            //append the public key file contents to authorized_keys file on remote host
            String mergeCommand = "cd .ssh; cat key.tmp >> " + AUTH_KEY_FILE;
            if(logger.isLoggable(Level.FINER)) {
                logger.finer("mergeCommand = " + mergeCommand);
            }
            if(conn.exec(mergeCommand, new ByteArrayOutputStream())!=0) {
                throw new IOException("Failed to propogate the public key " + pubKeyFile + " to " + host);
            }
            logger.info("Copied keyfile " + pubKeyFile + " to " + userName + "@" + host);

            //remove the public key file on remote host
            if(conn.exec("rm .ssh/key.tmp", new ByteArrayOutputStream())!=0) {
                logger.warning("WARNING: Failed to remove the public key file key.tmp on remote host " + host);
            }
            if(logger.isLoggable(Level.FINER)) {
                logger.finer("Removed the temporary key file on remote host");
            }
           
            //Lets fix all the permissions
            //On MKS, chmod doesn't work as expected. StrictMode needs to be disabled
            //for connection to go through
            logger.info("Fixing file permissions for home(755), .ssh(700) and authorized_keys file(644)");
            sftp.chmod(".", 0755);
            sftp.chmod(SSH_DIR, 0700);
            sftp.chmod(SSH_DIR + AUTH_KEY_FILE, 0644);
            connection.close();
            conn.close();
        }
    }
View Full Code Here

     * Check if we can authenticate using public key auth
     * @return true|false
     */
    public boolean checkConnection() {
        boolean status = false;
        Connection c = null;
        c = new Connection(host, port);
        try {
            c.connect();
            File f = new File(keyFile);
            if(logger.isLoggable(Level.FINER)) {
                logger.finer("Checking connection...");
            }
            status = c.authenticateWithPublicKey(userName, f, rawKeyPassPhrase);
            if (status) {
                logger.info("Successfully connected to " + userName + "@" + host + " using keyfile " + keyFile);
            }
        } catch(IOException ioe) {
            Throwable t = ioe.getCause();
            if (t != null) {
                String msg = t.getMessage();
                logger.warning("Failed to connect or authenticate: " + msg);
            }
            if (logger.isLoggable(Level.FINER)) {
                logger.log(Level.FINER, "Failed to connect or autheticate: ", ioe);
            }
        } finally {
            c.close();
        }
        return status;
    }
View Full Code Here

TOP

Related Classes of vicazh.hyperpool.stream.net.pop3.Connection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.