Jump to content

PERL: Use modules on remote servers..

- - - - -

  • Please log in to reply
No replies to this topic

#1
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
So I usually email useful code that I wrote to myself so I can reference it later at work, but I suppose I can just throw it on threads here so people can use it as well.

Anywho, at work we have like 400 servers that need to interface with a custom PHP based website I created, via XML API, my language of choice... Perl!

So this isnt what its used for, but ill come up with a fake example.

400 servers, need to run a perl script once a day to interface with the remote PHP server to update stats or something like that.. whatever. The problem is.. what if you want to update the perl code? You going to go to 400 servers? Even if you write a quick shell script to update them on EVERY server... still gotta be an easier way. I started to think, what if I can make it so they call a custom perl package I write.... on a remote machine!...

Well I did it, and it works great. Here is an example.

This SAM.pm is on a remote server, accessible via port 80 (for security reasons, only to the network allowed to run these scripts, or you can use the credentials feature in LWP::UserAgent to have it authenticate via apache auth), at http://somebswebsite.../modules/SAM.pm
package SAM;
# By Justin H
use strict;

use constant TRUE => 1;
use constant FALSE => 0;

sub main {
    my ($self,$value,@args) = @_;

    if (($value =~ /^(\d+\.?\d*|\.\d+)$/) && ($value > 10)) { 
        return TRUE;
    }
    else {
        return FALSE;
    }
};

1;
All this is, is a package named SAM, with a subroutine called main, that will look at $value to make sure its numeric, and greater than 10, if so, return true, if not, return false... pretty basic.

Now this perl script is on the machine that executes the script... It will call http://somebswebsite.../modules/SAM.pm, and use it..
#!/usr/bin/perl
use strict;
use warnings;

BEGIN {
    my $LWP = eval { require LWP::UserAgent; };
    if(!$LWP) {
        
        print "Perl module LWP::UserAgent is not installed on this server, it must be installed to use the remote SAM modules.\n";
        exit 1;
    } 
    else {
        use LWP::UserAgent;
    }
    
    push @INC, sub {
        my $URL = 'http://micro.aznetworkuddptime.com/SAM/SAM.pm';
        my $ua = LWP::UserAgent->new;
        $ua->timeout(10);
        my $module = $ua->get($URL); 
        if($module->is_success) {
            my $package = $module->content;
            open my $fh, '<', \$package or die $!;
            return $fh;
        }
        else {
            print "LWP::UserAgent failed: ". $module->status_line."\n";
            exit 1;
        }
        
    }
}

use SAM;


my $number = 20;

my $result = SAM->main($number);

if ($result) {
    print "$number is greater than 10!\n";
} 
else {
    print "$number is less than 10 or not numeric\n";
}
Too lazy to actually explain to you how it works, maybe ill update it later.

Hopefully this will save you perl kids some time... not that anyone on here uses perl.
Checkout my new forum! http://adminreference.com/




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users