The -h switch will show usage.
I've attached the file but I suspect that will it be lost in the mailing list archives.
#!/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;
}