Search in sources :

Example 71 with Session

use of com.trilead.ssh2.Session in project intellij-community by JetBrains.

the class SshSessionConnection method open.

public void open(IStreamLogger streamLogger) throws AuthenticationException {
    SshLogger.debug("opening session");
    mySession = mySessionProvider.compute();
    // wrapper created, inspection is inapplicable
    //noinspection IOResourceOpenedButNotSafelyClosed
    myInputStream = new MyInputStreamWrapper(myActivityMonitor, mySession.getStdout());
    // wrapper created, inspection is inapplicable
    //noinspection IOResourceOpenedButNotSafelyClosed
    myOutputStream = new MyOutputStreamWrapper(myActivityMonitor, mySession.getStdin());
    myErrorStreamGobbler = new StreamGobbler(mySession.getStderr());
    myState = LifeStages.CREATED;
}
Also used : StreamGobbler(com.trilead.ssh2.StreamGobbler)

Example 72 with Session

use of com.trilead.ssh2.Session in project ACS by ACS-Community.

the class Executor method remoteDownAllPortable.

/**
	 * Shuts down all ssh-sessions and -connections that may still be active.
	 */
private static void remoteDownAllPortable() {
    for (Session sess : sessions) {
        try {
            sess.close();
            log.fine("closed " + sess);
        } catch (Exception exc) {
            log.fine("could not close " + sess);
        }
    }
    for (Connection conn : connections) {
        try {
            conn.close();
            log.fine("closed " + conn);
        } catch (Exception exc) {
            log.fine("could not close " + conn);
        }
    }
}
Also used : Connection(com.trilead.ssh2.Connection) IOException(java.io.IOException) OrbInitException(alma.acs.commandcenter.meta.Firestarter.OrbInitException) AcsJException(alma.acs.exceptions.AcsJException) Session(com.trilead.ssh2.Session)

Example 73 with Session

use of com.trilead.ssh2.Session in project pentaho-kettle by pentaho.

the class SSH method processRow.

@Override
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (SSHMeta) smi;
    data = (SSHData) sdi;
    Object[] row;
    if (meta.isDynamicCommand()) {
        row = getRow();
        if (row == null) {
            setOutputDone();
            return false;
        }
        if (first) {
            first = false;
            data.outputRowMeta = getInputRowMeta().clone();
            data.nrInputFields = data.outputRowMeta.size();
            meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
            data.nrOutputFields = data.outputRowMeta.size();
            // Check if commands field is provided
            if (meta.isDynamicCommand()) {
                if (Utils.isEmpty(meta.getcommandfieldname())) {
                    throw new KettleException(BaseMessages.getString(PKG, "SSH.Error.CommandFieldMissing"));
                }
                // cache the position of the source filename field
                data.indexOfCommand = data.outputRowMeta.indexOfValue(meta.getcommandfieldname());
                if (data.indexOfCommand < 0) {
                    // The field is unreachable !
                    throw new KettleException(BaseMessages.getString(PKG, "SSH.Exception.CouldnotFindField", meta.getcommandfieldname()));
                }
            }
        }
    } else {
        if (!data.wroteOneRow) {
            // empty row
            row = new Object[] {};
            incrementLinesRead();
            data.wroteOneRow = true;
            if (first) {
                first = false;
                data.outputRowMeta = new RowMeta();
                data.nrInputFields = 0;
                meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
                data.nrOutputFields = data.outputRowMeta.size();
                data.commands = environmentSubstitute(meta.getCommand());
            }
        } else {
            // signal end to receiver(s)
            setOutputDone();
            return false;
        }
    }
    RowMetaInterface imeta = getInputRowMeta();
    if (imeta == null) {
        imeta = new RowMeta();
        this.setInputRowMeta(imeta);
    }
    // Reserve room
    Object[] rowData = new Object[data.nrOutputFields];
    for (int i = 0; i < data.nrInputFields; i++) {
        // no data is changed, clone is not needed here.
        rowData[i] = row[i];
    }
    int index = data.nrInputFields;
    Session session = null;
    try {
        if (meta.isDynamicCommand()) {
            // get commands
            data.commands = data.outputRowMeta.getString(row, data.indexOfCommand);
            if (Utils.isEmpty(data.commands)) {
                throw new KettleException(BaseMessages.getString(PKG, "SSH.Error.MessageEmpty"));
            }
        }
        // Open a session
        session = data.conn.openSession();
        if (log.isDebug()) {
            logDebug(BaseMessages.getString(PKG, "SSH.Log.SessionOpened"));
        }
        // execute commands
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "SSH.Log.RunningCommand", data.commands));
        }
        session.execCommand(data.commands);
        // Read Stdout, Sterr and exitStatus
        SessionResult sessionresult = new SessionResult(session);
        if (log.isDebug()) {
            logDebug(BaseMessages.getString(PKG, "SSH.Log.CommandRunnedCommand", data.commands, sessionresult.getStdOut(), sessionresult.getStdErr()));
        }
        // Add stdout to output
        rowData[index++] = sessionresult.getStd();
        if (!Utils.isEmpty(data.stdTypeField)) {
            // Add stdtype to output
            rowData[index++] = sessionresult.isStdTypeErr();
        }
        if (log.isRowLevel()) {
            logRowlevel(BaseMessages.getString(PKG, "SSH.Log.OutputLine", data.outputRowMeta.getString(rowData)));
        }
        putRow(data.outputRowMeta, rowData);
        if (checkFeedback(getLinesRead())) {
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "SSH.LineNumber", "" + getLinesRead()));
            }
        }
    } catch (Exception e) {
        boolean sendToErrorRow = false;
        String errorMessage = null;
        if (getStepMeta().isDoingErrorHandling()) {
            sendToErrorRow = true;
            errorMessage = e.toString();
        } else {
            logError(BaseMessages.getString(PKG, "SSH.ErrorInStepRunning") + e.getMessage());
            setErrors(1);
            stopAll();
            // signal end to receiver(s)
            setOutputDone();
            return false;
        }
        if (sendToErrorRow) {
            // Simply add this row to the error row
            putError(getInputRowMeta(), row, 1, errorMessage, null, "SSH001");
        }
    } finally {
        if (session != null) {
            session.close();
            if (log.isDebug()) {
                logDebug(BaseMessages.getString(PKG, "SSH.Log.SessionClosed"));
            }
        }
    }
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) RowMeta(org.pentaho.di.core.row.RowMeta) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) KettleException(org.pentaho.di.core.exception.KettleException) Session(com.trilead.ssh2.Session)

Example 74 with Session

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

Example 75 with Session

use of com.trilead.ssh2.Session in project Payara by payara.

the class SSHLauncher method runCommandAsIs.

private int runCommandAsIs(String command, OutputStream os, List<String> stdinLines) throws IOException, InterruptedException {
    if (logger.isLoggable(Level.FINER)) {
        logger.finer("Running command " + command + " on host: " + this.host);
    }
    openConnection();
    final Session sess = connection.openSession();
    int status = exec(sess, command, os, listInputStream(stdinLines));
    // XXX: Should we close connection after each command or cache it
    // and re-use it?
    SSHUtil.unregister(connection);
    connection = null;
    return status;
}
Also used : 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)31 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