Search in sources :

Example 1 with Session

use of net.schmizz.sshj.connection.channel.direct.Session in project asterixdb by apache.

the class AbstractRemoteExecutableAction method doExecute.

@Override
protected boolean doExecute(String command, Map<String, String> env) throws Exception {
    int exitVal = 0;
    client.loadKnownHosts();
    try {
        client.connect(hostname, port);
        client.authPublickey(username, keyLocation);
        Session s = client.startSession();
        try {
            for (Entry<String, String> e : env.entrySet()) {
                s.setEnvVar(e.getKey(), e.getValue());
            }
            cmd = s.exec(command);
            cmd.join();
            Integer ev = cmd.getExitStatus();
            exitVal = ev == null ? -1 : ev;
            cmd.close();
        } finally {
            s.close();
        }
    } finally {
        client.close();
    }
    return exitVal == 0;
}
Also used : Session(net.schmizz.sshj.connection.channel.direct.Session)

Example 2 with Session

use of net.schmizz.sshj.connection.channel.direct.Session in project asterixdb by apache.

the class StartDataGeneratorAction method main.

public static void main(String[] args) throws Exception {
    SSHClient sshClient = new SSHClient();
    sshClient.loadKnownHosts();
    sshClient.connect("asterix-1.ics.uci.edu");
    sshClient.authPublickey("zheilbro", "/Users/zheilbron/.ssh/id_rsa");
    Session session = sshClient.startSession();
    Command lsCmd = session.exec("ls");
    StringWriter sw = new StringWriter();
    IOUtils.copy(lsCmd.getInputStream(), sw);
    IOUtils.copy(lsCmd.getErrorStream(), sw);
    System.out.println(sw.toString());
    session.close();
    sw.close();
    sshClient.close();
}
Also used : StringWriter(java.io.StringWriter) Command(net.schmizz.sshj.connection.channel.direct.Session.Command) SSHClient(net.schmizz.sshj.SSHClient) Session(net.schmizz.sshj.connection.channel.direct.Session)

Example 3 with Session

use of net.schmizz.sshj.connection.channel.direct.Session in project vcell by virtualcell.

the class CommandServiceSsh_sshj method command.

@Override
public CommandOutput command(String[] commandStrings, int[] allowableReturnCodes) throws ExecutableException {
    if (allowableReturnCodes == null) {
        throw new IllegalArgumentException("allowableReturnCodes must not be null");
    }
    long timeMS = System.currentTimeMillis();
    SshConnection sshConnection = null;
    try {
        sshConnection = pool.borrowObject();
        try (Session session = sshConnection.client.startSession()) {
            String cmd = CommandOutput.concatCommandStrings(commandStrings);
            Session.Command command = session.exec(cmd);
            // wait up to a minute for return -- will return sooner if command completes
            command.join(1, TimeUnit.MINUTES);
            String standardOutput = IOUtils.readFully(command.getInputStream()).toString();
            String standardError = IOUtils.readFully(command.getErrorStream()).toString();
            command.close();
            Integer exitStatus = command.getExitStatus();
            long elapsedTimeMS = System.currentTimeMillis() - timeMS;
            CommandOutput commandOutput = new CommandOutput(commandStrings, standardOutput, standardError, exitStatus, elapsedTimeMS);
            if (VCMongoMessage.enabled) {
                VCMongoMessage.sendCommandServiceCall(commandOutput);
            }
            if (commandOutput.getExitStatus() == null) {
                lg.error("Command: " + commandOutput.getCommand());
                lg.error("Command: exit error message: " + command.getExitErrorMessage());
                lg.error("Command: exit signal: " + command.getExitSignal());
                lg.error("Command: exit was core dumped: " + command.getExitWasCoreDumped());
            } else {
                lg.debug("Command: " + commandOutput.getCommand());
                lg.trace("Command: stdout = " + commandOutput.getStandardOutput());
                lg.trace("Command: stderr = " + commandOutput.getStandardError());
                lg.debug("Command: exit = " + commandOutput.getExitStatus());
            }
            boolean bReturnCodeAllowable = false;
            for (int returnCode : allowableReturnCodes) {
                if (commandOutput.getExitStatus() != null && returnCode == commandOutput.getExitStatus().intValue()) {
                    bReturnCodeAllowable = true;
                }
            }
            if (!bReturnCodeAllowable) {
                lg.error("Command: " + commandOutput.getCommand());
                lg.error("Command: stdout = " + commandOutput.getStandardOutput());
                lg.error("Command: stderr = " + commandOutput.getStandardError());
                lg.error("Command: exit = " + commandOutput.getExitStatus());
                throw new ExecutableException("command exited with return code = " + commandOutput.getExitStatus());
            }
            return commandOutput;
        } catch (Exception e) {
            lg.error("command failed", e);
            throw new ExecutableException(e.getMessage(), e);
        }
    } catch (ExecutableException e) {
        throw e;
    } catch (Exception e) {
        lg.error("failed to borrow ssh connection from pool", e);
        if (sshConnection != null) {
            sshConnection.setFailed();
        }
        throw new ExecutableException(e.getMessage(), e);
    } finally {
        if (sshConnection != null) {
            try {
                pool.returnObject(sshConnection);
            } catch (Exception e) {
                lg.error("failed to return ssh connection to pool", e);
                e.printStackTrace();
            }
        }
    }
}
Also used : ExecutableException(org.vcell.util.exe.ExecutableException) Command(net.schmizz.sshj.connection.channel.direct.Session.Command) ConnectionException(net.schmizz.sshj.connection.ConnectionException) TransportException(net.schmizz.sshj.transport.TransportException) IOException(java.io.IOException) ExecutableException(org.vcell.util.exe.ExecutableException) Session(net.schmizz.sshj.connection.channel.direct.Session)

Aggregations

Session (net.schmizz.sshj.connection.channel.direct.Session)3 Command (net.schmizz.sshj.connection.channel.direct.Session.Command)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 SSHClient (net.schmizz.sshj.SSHClient)1 ConnectionException (net.schmizz.sshj.connection.ConnectionException)1 TransportException (net.schmizz.sshj.transport.TransportException)1 ExecutableException (org.vcell.util.exe.ExecutableException)1