use of com.google.gerrit.sshd.SshSession 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");
}
}
}
use of com.google.gerrit.sshd.SshSession 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");
}
Aggregations