Search in sources :

Example 1 with IoAcceptor

use of org.apache.sshd.common.io.IoAcceptor in project gerrit by GerritCodeReview.

the class CloseConnection method run.

@Override
protected void run() throws Failure {
    IoAcceptor acceptor = sshDaemon.getIoAcceptor();
    if (acceptor == null) {
        throw new Failure(1, "fatal: sshd no longer running");
    }
    for (String sessionId : sessionIds) {
        boolean connectionFound = false;
        int id = (int) Long.parseLong(sessionId, 16);
        for (IoSession io : acceptor.getManagedSessions().values()) {
            AbstractSession serverSession = AbstractSession.getSession(io, true);
            SshSession sshSession = serverSession != null ? serverSession.getAttribute(SshSession.KEY) : null;
            if (sshSession != null && sshSession.getSessionId() == id) {
                connectionFound = true;
                stdout.println("closing connection " + sessionId + "...");
                CloseFuture future = io.close(true);
                if (wait) {
                    try {
                        future.await();
                        stdout.println("closed connection " + sessionId);
                    } catch (IOException e) {
                        log.warn("Wait for connection to close interrupted: " + e.getMessage());
                    }
                }
                break;
            }
        }
        if (!connectionFound) {
            stderr.print("close connection " + sessionId + ": no such connection\n");
        }
    }
}
Also used : CloseFuture(org.apache.sshd.common.future.CloseFuture) IoAcceptor(org.apache.sshd.common.io.IoAcceptor) IOException(java.io.IOException) SshSession(com.google.gerrit.sshd.SshSession) IoSession(org.apache.sshd.common.io.IoSession) AbstractSession(org.apache.sshd.common.session.helpers.AbstractSession)

Example 2 with IoAcceptor

use of org.apache.sshd.common.io.IoAcceptor in project gerrit by GerritCodeReview.

the class ShowCaches method sshSummary.

private void sshSummary() {
    IoAcceptor acceptor = daemon.getIoAcceptor();
    if (acceptor == null) {
        return;
    }
    long now = TimeUtil.nowMs();
    Collection<IoSession> list = acceptor.getManagedSessions().values();
    long oldest = now;
    for (IoSession s : list) {
        if (s instanceof MinaSession) {
            MinaSession minaSession = (MinaSession) s;
            oldest = Math.min(oldest, minaSession.getSession().getCreationTime());
        }
    }
    stdout.format("SSH:   %4d  users, oldest session started %s ago\n", list.size(), uptime(now - oldest));
}
Also used : IoAcceptor(org.apache.sshd.common.io.IoAcceptor) MinaSession(org.apache.sshd.common.io.mina.MinaSession) IoSession(org.apache.sshd.common.io.IoSession)

Example 3 with IoAcceptor

use of org.apache.sshd.common.io.IoAcceptor in project gerrit by GerritCodeReview.

the class ShowConnections method run.

@Override
protected void run() throws Failure {
    final IoAcceptor acceptor = daemon.getIoAcceptor();
    if (acceptor == null) {
        throw new Failure(1, "fatal: sshd no longer running");
    }
    final List<IoSession> list = new ArrayList<>(acceptor.getManagedSessions().values());
    Collections.sort(list, new Comparator<IoSession>() {

        @Override
        public int compare(IoSession arg0, IoSession arg1) {
            if (arg0 instanceof MinaSession) {
                MinaSession mArg0 = (MinaSession) arg0;
                MinaSession mArg1 = (MinaSession) arg1;
                if (mArg0.getSession().getCreationTime() < mArg1.getSession().getCreationTime()) {
                    return -1;
                } else if (mArg0.getSession().getCreationTime() > mArg1.getSession().getCreationTime()) {
                    return 1;
                }
            }
            return (int) (arg0.getId() - arg1.getId());
        }
    });
    hostNameWidth = wide ? Integer.MAX_VALUE : columns - 9 - 9 - 10 - 32;
    if (getBackend().equals("mina")) {
        long now = TimeUtil.nowMs();
        stdout.print(String.format("%-8s %8s %8s   %-15s %s\n", "Session", "Start", "Idle", "User", "Remote Host"));
        stdout.print("--------------------------------------------------------------\n");
        for (final IoSession io : list) {
            checkState(io instanceof MinaSession, "expected MinaSession");
            MinaSession minaSession = (MinaSession) io;
            long start = minaSession.getSession().getCreationTime();
            long idle = now - minaSession.getSession().getLastIoTime();
            AbstractSession s = AbstractSession.getSession(io, true);
            SshSession sd = s != null ? s.getAttribute(SshSession.KEY) : null;
            stdout.print(String.format("%8s %8s %8s   %-15.15s %s\n", id(sd), time(now, start), age(idle), username(sd), hostname(io.getRemoteAddress())));
        }
    } else {
        stdout.print(String.format("%-8s   %-15s %s\n", "Session", "User", "Remote Host"));
        stdout.print("--------------------------------------------------------------\n");
        for (final IoSession io : list) {
            AbstractSession s = AbstractSession.getSession(io, true);
            SshSession sd = s != null ? s.getAttribute(SshSession.KEY) : null;
            stdout.print(String.format("%8s   %-15.15s %s\n", id(sd), username(sd), hostname(io.getRemoteAddress())));
        }
    }
    stdout.print("--\n");
    stdout.print("SSHD Backend: " + getBackend() + "\n");
}
Also used : IoAcceptor(org.apache.sshd.common.io.IoAcceptor) ArrayList(java.util.ArrayList) MinaSession(org.apache.sshd.common.io.mina.MinaSession) SshSession(com.google.gerrit.sshd.SshSession) IoSession(org.apache.sshd.common.io.IoSession) AbstractSession(org.apache.sshd.common.session.helpers.AbstractSession)

Aggregations

IoAcceptor (org.apache.sshd.common.io.IoAcceptor)3 IoSession (org.apache.sshd.common.io.IoSession)3 SshSession (com.google.gerrit.sshd.SshSession)2 MinaSession (org.apache.sshd.common.io.mina.MinaSession)2 AbstractSession (org.apache.sshd.common.session.helpers.AbstractSession)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CloseFuture (org.apache.sshd.common.future.CloseFuture)1