#!/usr/bin/perl -w # # Gets the room stats for bc07 and extracts a random subset of available hosts. # use strict; use HTTP::Request::Common; use HTTP::Response; use LWP::UserAgent; my $pctype = "linux"; # Which pcs to display my $sample_size = "10"; # First, get the sample size and whether to get linux pcs or windows pcs if( $#ARGV == 1 ) { if( $ARGV[0] eq "linux" ) { $pctype = "linux"; } elsif ( $ARGV[0] eq "windows" ) { $pctype = "windows"; } else { usage(); die "You have to choose to display either windows or linux pcs\n"; } $sample_size = 0 + $ARGV[1]; if( $sample_size <= 0 ) { usage(); die "You have to specify a positive sample size\n"; } } # Now get the webpage my $ua = LWP::UserAgent->new; my $icbc_url = "http://ic-it.epfl.ch/it/sallesinfo/bc0708/bc0708_users.html"; my $response = $ua->request(GET $icbc_url); if ( $response->is_success ) { my $current_pc; my @windows_pcs = (); my @linux_pcs = (); for my $line (split("\n",$response->content)) { if( $line =~ m!([^<]*)! ) { $current_pc = $1; } if( $line =~ /Windows/ ) { push @windows_pcs,$current_pc; } if( $line =~ /Linux/ ) { push @linux_pcs,$current_pc; } } my @sample = (); if( $pctype eq "linux" ) { @sample = get_sample($sample_size,@linux_pcs); } else { @sample = get_sample($sample_size,@windows_pcs); } print join("\n",@sample); print "\n"; } else { die "Failed to download computer listing"; } sub get_sample { my $size = shift; my @elems = @_; if( $#elems < $size ) { $size = $#elems + 1; # We can't deliver as many elements as we want. } my @ret = (); # Now permute the first $size elements for( my $i = 0; $i < $size; $i++ ) { my $rpos = int(rand($#elems - $size)); push @ret,$elems[$rpos + $i]; $elems[$rpos + $i] = $elems[$i]; # The unused element at the old position } return @ret; } sub usage { print "usage: ./stats.pl windows|linux 10\n"; }