Search in sources :

Example 71 with Session

use of org.neo4j.driver.v1.Session in project intellij-community by JetBrains.

the class PublicKeyAuthentication method main.

public static void main(String[] args) {
    String hostname = "127.0.0.1";
    String username = "joe";
    // or "~/.ssh/id_dsa"
    File keyfile = new File("~/.ssh/id_rsa");
    // will be ignored if not needed
    String keyfilePass = "joespass";
    try {
        /* Create a connection instance */
        Connection conn = new Connection(hostname);
        /* Now connect */
        conn.connect();
        /* Authenticate */
        boolean isAuthenticated = conn.authenticateWithPublicKey(username, keyfile, keyfilePass);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("uname -a && date && uptime && who");
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        System.out.println("Here is some information about the remote host:");
        while (true) {
            String line = br.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        /* Close this session */
        sess.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : StreamGobbler(com.trilead.ssh2.StreamGobbler) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 72 with Session

use of org.neo4j.driver.v1.Session in project intellij-community by JetBrains.

the class UsingKnownHosts method main.

public static void main(String[] args) throws IOException {
    String hostname = "somehost";
    String username = "joe";
    String password = "joespass";
    File knownHosts = new File("~/.ssh/known_hosts");
    try {
        if (knownHosts.exists())
            database.addHostkeys(knownHosts);
        /* Create a connection instance */
        Connection conn = new Connection(hostname);
        /* Now connect and use the SimpleVerifier */
        conn.connect(new SimpleVerifier(database));
        /* Authenticate */
        boolean isAuthenticated = conn.authenticateWithPassword(username, password);
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        /* Create a session */
        Session sess = conn.openSession();
        sess.execCommand("uname -a && date && uptime && who");
        InputStream stdout = new StreamGobbler(sess.getStdout());
        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
        System.out.println("Here is some information about the remote host:");
        while (true) {
            String line = br.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        /* Close this session */
        sess.close();
        /* Close the connection */
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }
}
Also used : StreamGobbler(com.trilead.ssh2.StreamGobbler) Connection(com.trilead.ssh2.Connection) Session(com.trilead.ssh2.Session)

Example 73 with Session

use of org.neo4j.driver.v1.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 74 with Session

use of org.neo4j.driver.v1.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 75 with Session

use of org.neo4j.driver.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)30 Driver (org.neo4j.driver.v1.Driver)29 InputStream (java.io.InputStream)28 StatementResult (org.neo4j.driver.v1.StatementResult)20 Record (org.neo4j.driver.v1.Record)15 Session (iaik.pkcs.pkcs11.Session)10 TokenException (iaik.pkcs.pkcs11.TokenException)10 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)10 P11TokenException (org.xipki.security.exception.P11TokenException)10 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)8 SCPClient (com.trilead.ssh2.SCPClient)6