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