Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Nothing special. Isn't that what Perls all about?

by odie (Sexton)
on May 23, 2001 at 23:49 UTC ( #82697=CUFP: print w/replies, xml ) Need Help??

This could be a bother to write in any other language, but Perl makes it so easy that it would be silly not to build it. It is a simple tool.
It does only this: Grab the web page containing the load stats on the login machines at my university. Select the server with lowest load and launch ssh.
I for one feel taht this is the biggest use for Perl. Those small nice hacks that just make life a bit... smoother. Imagine how long this little widget would be written in any other language.

#!/usr/bin/perl -w if ( $#ARGV != 0 ) { print "efd v1.0 by d99msr\n"; print "Usage:\n"; print " efd username\n"; exit; } my $user = $ARGV[0]; use HTTP::Request::Common; use LWP::UserAgent; print "Retrieving and parsing load average stats.\n"; my $ua = LWP::UserAgent->new; my $req = GET ''; my @raw = split("\n", $ua->request($req)->as_string); my $lowest = 10; my $machine; foreach (@raw) { if ( $_ =~ m/TR.*TD.*TD.*TD.*TD.*TD.*TD.*TD.*TD.*TD.*TD.*TD.*TD.*TR/) + { $_ =~ s/\<.*?\>\<.*?\>/\<\>/ig; $_ =~ s/ //g; @split = split("<>", $_); if ( $split[5] < $lowest ) { $lowest = $split[5]; $machine = $split[1]; } } } print "Connecting to $machine, load average: $lowest\n"; my $efdname = "$user\@$"; exec("ssh $efdname"); exit;


I am a manual signature virus. Copy me please!

Replies are listed 'Best First'.
Re: Nothing special. Isn't that what Perls all about?
by merlyn (Sage) on May 24, 2001 at 00:28 UTC
    Or, more succinctly:
    #!/usr/bin/perl use LWP::Simple; use HTML::TableExtract; my $html = get "" or die "Cannot get!\n"; my $te = HTML::TableExtract->new(headers => ['Name', 'Load average']); $te->parse($html)->eof; my @sorted = sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] } $te-> +rows; for (@sorted) { print "$_->[0] has load of $_->[1]\n"; } print "exec ssh $sorted[0][0]\n";

    -- Randal L. Schwartz, Perl hacker

Re: Nothing special. Isn't that what Perls all about?
by btrott (Parson) on May 24, 2001 at 00:45 UTC
    I'm no regex whiz, but I think you're making Perl do a lot of extra work with that regex to match table rows. All of those dot-stars means a lot of backtracking, I think.

    Anyway, here's a slightly modified version:

    use LWP::Simple; my $user = shift or die "usage: $0 <username>"; $_ = get "" or die "Cannot get!\n"; my $re = '<TR ALIGN=center>' . ('<TD>\s*([^<]+)</TD>' x 6) . '</TR +>'; my($load, $host); while (/$re/g) { $host = $1, $load = $5 if !defined $load || $5 < $load; } print "Host $host has load of $load\n"; exec("ssh $user\@$") or die "Can't exec ssh: $!";
    Note also that I took out the exit after the exec--you don't need that, because exec never returns.

    I think my regex might be a little more friendly, but I'm not absolutely sure. Note also that any regex solution will be less robust and less succeptible to HTML formatting changes than a solution like merlyn's.

    Updated to test return value from exec, because I should've been more clear about exec and the possibility that it could return.

      exec returns if the program to be execed is not there.
Re: Nothing special. Isn't that what Perls all about?
by weingart (Acolyte) on May 24, 2001 at 23:20 UTC
    Perl is good, Perl is great, why not just Unix we appreciate?
    ssh `lynx -dump | grep Ul | tr - +s ' ' | cut -d' ' -f2,8 | sort -n -k 2 | head -1 | cut -d' ' -f1`

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://82697]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2020-12-04 20:31 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (62 votes). Check out past polls.