Search in sources :

Example 1 with ChannelSession

use of org.apache.sshd.client.channel.ChannelSession in project platformlayer by platformlayer.

the class MinaSshConnectionWrapper method openSession.

public ChannelSession openSession(String command) throws SshException {
    try {
        boolean useAgentForwarding = sshContext.isAgentForwarding();
        ChannelSession clientChannel = BugFixChannelExec.createExecChannel(sshClientSession, command, useAgentForwarding);
        return clientChannel;
    } catch (Exception e) {
        ExceptionUtils.handleInterrupted(e);
        throw new SshException("Error creating channel", e);
    }
}
Also used : ChannelSession(org.apache.sshd.client.channel.ChannelSession) SshException(org.platformlayer.ops.ssh.SshException) IOException(java.io.IOException) SshException(org.platformlayer.ops.ssh.SshException)

Example 2 with ChannelSession

use of org.apache.sshd.client.channel.ChannelSession in project platformlayer by platformlayer.

the class MinaSshConnection method sshExecute.

int sshExecute(String command, final OutputStream stdout, final OutputStream stderr, ProcessStartListener listener, TimeSpan timeout) throws SshException, IOException, InterruptedException {
    ChannelSession sshChannel = null;
    try {
        sshChannel = ensureConnected().openSession(command);
        sshChannel.setIn(new ByteArrayInputStream(new byte[0]));
        sshChannel.setOut(stdout);
        sshChannel.setErr(stderr);
        try {
            sshChannel.open().await(DEFAULT_SSH_CONNECT_TIMEOUT.getTotalMilliseconds());
        } catch (Exception e) {
            ExceptionUtils.handleInterrupted(e);
            throw new SshException("Ssh error opening channel", e);
        }
        if (listener != null) {
            throw new UnsupportedOperationException();
        // listener.startedProcess(sshChannel.getStdin());
        }
        if (timeout == null) {
            timeout = TimeSpan.ZERO;
        }
        // Wait for everything to finish
        int flags = sshChannel.waitFor(ClientChannel.EOF | ClientChannel.CLOSED, timeout.getTotalMilliseconds());
        if ((flags & ClientChannel.TIMEOUT) != 0) {
            closeAndRemoveFromPool();
            throw new SshException("Timeout while waiting for SSH task to complete.  Timeout was " + timeout);
        }
        flags = sshChannel.waitFor(ClientChannel.EXIT_STATUS, 30000);
        if ((flags & ClientChannel.TIMEOUT) != 0) {
            closeAndRemoveFromPool();
            throw new SshException("Timeout while waiting for exit code.  Timeout was " + timeout);
        }
        Integer exitCode = getExitStatus(sshChannel);
        if (exitCode == null) {
            closeAndRemoveFromPool();
            throw new SshException("No exit code returned");
        }
        return exitCode;
    } finally {
        if (sshChannel != null) {
            sshChannel.close(false);
        }
    }
}
Also used : ChannelSession(org.apache.sshd.client.channel.ChannelSession) ByteArrayInputStream(java.io.ByteArrayInputStream) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) SshException(org.platformlayer.ops.ssh.SshException) RuntimeSshException(org.apache.sshd.common.RuntimeSshException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 ChannelSession (org.apache.sshd.client.channel.ChannelSession)2 SshException (org.platformlayer.ops.ssh.SshException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 RuntimeSshException (org.apache.sshd.common.RuntimeSshException)1