public
Last active

Perl script to assist with connecting to a UTCS Unix machine

  • Download Gist
unixlab.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#!/usr/bin/env perl
use strict;
use warnings;
use 5.012;
use feature qw(switch say);
use LWP::Simple;
 
my $url = "http://apps.cs.utexas.edu/unixlabstatus/";
 
my $html = get $url;
 
die "Cannot connect to $url\n" unless defined $html;
 
my @lines = split /\n/, $html;
 
my @table = grep /<td class="ruptime"/, @lines;
 
my @stations;
my $station_index = 0;
 
while (my ($i, $value) = each @table) {
$value =~ s#^.*?>(.*)</td>#$1#;
 
given ($i % 5) {
when (0) {
$station_index++ unless $i == 0;
 
push @stations, {};
$stations[$station_index]{host} = $value;
}
when (1) {
$stations[$station_index]{status} = $value;
}
when (2) {
$stations[$station_index]{uptime} = $value;
}
when (3) {
$stations[$station_index]{users} = $value;
}
when (4) {
$stations[$station_index]{load} = $value;
}
}
}
 
# Find machine with lowest load
my $min_load = $stations[0]{load};
my $min_index = 0;
 
while (my ($i, $station) = each @stations) {
next if $i == 0;
next if $station->{status} eq "down";
 
if ($station->{load} <= $min_load) {
$min_index = $i;
$min_load = $station->{load};
}
}
 
say "$stations[$min_index]{host}.cs.utexas.edu";

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.