[X2Go-User] Utility for cleaning x2go sessions
Norman Gaywood
ngaywood at une.edu.au
Tue Mar 7 02:53:31 CET 2017
I can have 50-60 users logged on via x2go. Sometimes I need to logout many
users. I use the following utility to list and match x2go sessions.
Using the -r switch will run x2goterminate-session against the matched
sessions.
Others may find this useful, so I post my script here.
Running the script without arguments will simply list all x2go sessions.
The -h switch will show usage.
I've attached the file but I suspect that will it be lost in the mailing
list archives.
Since it's short I also cut and paste here:
------------------------------ cut ------------------------------
#!/usr/bin/perl
# List (and terminate) x2go sessions
use strict;
use warnings;
use v5.10;
use Getopt::Std;
my %opts;
getopts('hjqr', \%opts);
my $help = defined $opts{h} ? $opts{h} : 0;
my $printjob = defined $opts{j} ? $opts{j} : 0;
my $quite = defined $opts{q} ? $opts{q} : 0;
my $run = defined $opts{r} ? $opts{r} : 0;
my $x2goterminate = '/usr/bin/x2goterminate-session';
my $x2golistsessions = '/usr/sbin/x2golistsessions_root';
if ($help) {
print STDERR <<EOT;
Usage: $0 [-hjqr] [[pattern] .. pattern]
Runs $x2golistsessions and outputs in a more readable format
Only print x2go sessions that match the pattern[s]
If no pattern is given, lists all x2go sessions
-h This message
-j print job command to terminate matched sessions, not session
information
-q quite, don't print anything
-r Terminate the matched sessions with $x2goterminate
EOT
exit 1;
}
# $x2golistsessions lists sessions like this:
#456|user-122-1460160892_stDXFCE_dp32|122|host.com
|R|2016-04-09T10:14:52|cba4aa51a29c4b7e2d529e6f628245eb|123.456.789.012|30259|30261|2016-04-09T10:14:56|user|1125|30262|
# 0 1 2 3 4 5
6 7 8 9 10
11 12 13
#
# These are the fields that we print (@pretty) and/or we match against
my @pretty = (4,5,7,1);
#
# The sessionID field required for $x2goterminate
my $sessionID = 1;
# Results sorted by time
my $sessionTime = 5;
# Generate the string we print and match against
sub pretty {
my $s = shift;
return "" unless defined $s; # (ref to) array of session fields
return sprintf "%s %s %-16s %s", @{$s}[@pretty];
}
# Get the session list and split the fields in each session
my $sessions = qx( $x2golistsessions );
my @sessions = ();
for my $s (split /\n/, $sessions) {
push @sessions, [ split '\|', $s ];
}
# If no command line patterns, match all sessions
unless (@ARGV) {
@ARGV = ( '.' );
}
# patten match on the session information
my %tokill; # sessions to kill/display
for my $p (@ARGV) {
my @tokill = grep { grep /$p/, pretty($_) } @sessions;
# convert to hash to eliminate duplicates over multiple @ARGV
for my $s (@tokill) {
$tokill{$s->[$sessionID]} = $s;
}
}
# pretty = session information that patterns are matched against
# job = command that would execute to terminate session
for my $s (sort {$a->[$sessionTime] cmp $b->[$sessionTime]} values %tokill)
{
my $pretty = pretty($s);
my $job = sprintf "%s %s", $x2goterminate, $s->[$sessionID];
unless ($quite) {
say $printjob ? $job : $pretty;
}
system $job if $run;
}
--
Norman Gaywood, Computer Systems Officer
School of Science and Technology
University of New England
Armidale NSW 2351, Australia
ngaywood at une.edu.au http://turing.une.edu.au/~ngaywood
Phone: +61 (0)2 6773 2412 Mobile: +61 (0)4 7862 0062
Please avoid sending me Word or Power Point attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.x2go.org/pipermail/x2go-user/attachments/20170307/6bc57d49/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: x2go_clean
Type: application/octet-stream
Size: 2698 bytes
Desc: not available
URL: <http://lists.x2go.org/pipermail/x2go-user/attachments/20170307/6bc57d49/attachment.obj>
More information about the x2go-user
mailing list