Search in sources :

Example 51 with Session

use of com.google.spanner.v1.Session in project zm-mailbox by Zimbra.

the class RemoteManager method executeBackground0.

private synchronized void executeBackground0(String command, RemoteBackgroundHandler handler) {
    Session s = null;
    try {
        s = getSession();
        if (ZimbraLog.rmgmt.isDebugEnabled())
            ZimbraLog.rmgmt.debug("(bg) executing shim command  '" + mShimCommand + "' on " + this);
        s.execCommand(mShimCommand);
        OutputStream os = s.getStdin();
        String send = "HOST:" + mHost + " " + command;
        if (ZimbraLog.rmgmt.isDebugEnabled())
            ZimbraLog.rmgmt.debug("(bg) sending mgmt command '" + send + "' on " + this);
        os.write(send.getBytes());
        os.close();
        InputStream stdout = new StreamGobbler(s.getStdout());
        InputStream stderr = new StreamGobbler(s.getStderr());
        handler.read(stdout, stderr);
    } catch (OutOfMemoryError e) {
        Zimbra.halt("out of memory", e);
    } catch (Throwable t) {
        handler.error(t);
    } finally {
        if (s != null) {
            releaseSession(s);
        }
    }
}
Also used : StreamGobbler(ch.ethz.ssh2.StreamGobbler) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Session(ch.ethz.ssh2.Session)

Example 52 with Session

use of com.google.spanner.v1.Session in project zm-mailbox by Zimbra.

the class RemoteManager method execute.

public synchronized RemoteResult execute(String command) throws ServiceException {
    Session s = null;
    try {
        s = getSession();
        if (ZimbraLog.rmgmt.isDebugEnabled())
            ZimbraLog.rmgmt.debug("executing shim command  '" + mShimCommand + "' on " + this);
        s.execCommand(mShimCommand);
        OutputStream os = s.getStdin();
        String send = "HOST:" + mHost + " " + command;
        if (ZimbraLog.rmgmt.isDebugEnabled()) {
            ZimbraLog.rmgmt.debug("sending mgmt command '%s' on %s", send, this);
        }
        os.write(send.getBytes());
        os.close();
        RemoteResult result = new RemoteResult();
        InputStream stdout = new StreamGobbler(s.getStdout());
        InputStream stderr = new StreamGobbler(s.getStderr());
        result.mStdout = ByteUtil.getContent(stdout, -1);
        result.mStderr = ByteUtil.getContent(stderr, -1);
        if (ZimbraLog.rmgmt.isTraceEnabled()) {
            try {
                ZimbraLog.rmgmt.trace("stdout content for cmd:\n%s", new String(result.mStdout, "UTF-8"));
                ZimbraLog.rmgmt.trace("stderr content for cmd:\n%s", new String(result.mStderr, "UTF-8"));
            } catch (Exception ex) {
                ZimbraLog.rmgmt.trace("Problem logging stdout or stderr for cmd - probably not UTF-8");
            }
        }
        try {
            result.mExitStatus = s.getExitStatus();
        } catch (NullPointerException npe) {
        // wow this is strange - on hold command we hit NPE here.  TODO file a bug against ganymed
        }
        if (result.mExitStatus != 0) {
            throw new IOException("command failed: exit status=" + result.mExitStatus + ", stdout=" + new String(result.mStdout) + ", stderr=" + new String(result.mStderr));
        }
        result.mExitSignal = s.getExitSignal();
        return result;
    } catch (IOException ioe) {
        throw ServiceException.FAILURE("exception executing command: " + command + " with " + this, ioe);
    } finally {
        if (s != null) {
            releaseSession(s);
        }
    }
}
Also used : StreamGobbler(ch.ethz.ssh2.StreamGobbler) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) IOException(java.io.IOException) ServiceException(com.zimbra.common.service.ServiceException) Session(ch.ethz.ssh2.Session)

Example 53 with Session

use of com.google.spanner.v1.Session in project CloudStack-archive by CloudStack-extras.

the class TestClientWithAPI 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://" + 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 (// 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 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 54 with Session

use of com.google.spanner.v1.Session in project CloudStack-archive by CloudStack-extras.

the class TestClientWithAPI method sshTest.

private static String sshTest(String host, String password, String snapshot_test) {
    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://" + downloadUrl + " && ls -al dump.bin";
            else
                linuxCommand = "wget http://" + downloadUrl + " && 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";
                }
            }
            if (snapshot_test.equals("no"))
                return result;
            else {
                Long sleep = 300000L;
                s_logger.info("Sleeping for " + sleep / 1000 / 60 + "minutes before executing next ssh test");
                Thread.sleep(sleep);
            }
        } catch (Exception e) {
            retry++;
            s_logger.error("SSH Linux Network test fail with error");
            if ((retry == MAX_RETRY_LINUX) && (snapshot_test.equals("no"))) {
                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 55 with Session

use of com.google.spanner.v1.Session in project CloudStack-archive by CloudStack-extras.

the class SSHCmdHelper method sshExecuteCmdOneShotWithExitCode.

public static int sshExecuteCmdOneShotWithExitCode(com.trilead.ssh2.Connection sshConnection, String cmd) throws sshException {
    s_logger.debug("Executing cmd: " + cmd);
    Session sshSession = null;
    try {
        sshSession = sshConnection.openSession();
        // There is a bug in Trilead library, wait a second before
        // starting a shell and executing commands, from http://spci.st.ewi.tudelft.nl/chiron/xref/nl/tudelft/swerl/util/SSHConnection.html
        Thread.sleep(1000);
        if (sshSession == null) {
            throw new sshException("Cannot open ssh session");
        }
        sshSession.execCommand(cmd);
        InputStream stdout = sshSession.getStdout();
        InputStream stderr = sshSession.getStderr();
        byte[] buffer = new byte[8192];
        while (true) {
            if (stdout == null || stderr == null) {
                throw new sshException("stdout or stderr of ssh session is null");
            }
            if ((stdout.available() == 0) && (stderr.available() == 0)) {
                int conditions = sshSession.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.");
                    break;
                }
                if ((conditions & ChannelCondition.EOF) != 0) {
                    if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0) {
                        break;
                    }
                }
            }
            while (stdout.available() > 0) {
                stdout.read(buffer);
            }
            while (stderr.available() > 0) {
                stderr.read(buffer);
            }
        }
        if (buffer[0] != 0)
            s_logger.debug(cmd + " output:" + new String(buffer));
        Thread.sleep(1000);
        return sshSession.getExitStatus();
    } catch (Exception e) {
        s_logger.debug("Ssh executed failed", e);
        throw new sshException("Ssh executed failed " + e.getMessage());
    } finally {
        if (sshSession != null)
            sshSession.close();
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Session(com.trilead.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