Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

WhichHost AKA pingtest.pl

by cybear (Monk)
on Apr 17, 2002 at 16:16 UTC ( [id://159873]=CUFP: print w/replies, xml ) Need Help??

I use this script, pingtest.pl, to find out which of my companies firewalls is ready for use. This script is designed to return a code (Green, Yellow, Red) and the name of the first reachable firewall server. usage: from some perl script ($result $availableFirewall) = `pingtest.pl callingscriptname`; (It just pings, it does not fully test connectivity) ((suggestions welcomed))
#!/bin/perl -w use Net::Telnet(); use strict; my $script = "$ARGV[0]"; my $availablefirewall = my $primaryfirewall = "main.firewall.com"; my $backupfirewall = "backup.firewall.com"; my $externalhost = "111.222.111.222"; my $key = ""; my @errors = ""; my @message = ""; ##################################### ###ping primary firewall server ### ##################################### my $command = "/usr/sbin/ping $availablefirewall"; my $pingfirewall = `$command`; unless ($pingfirewall =~ /alive/) { @errors = ("Cannot reach primary firewall $primaryfirewall...T +rying backup firewall $backupfirewall\n"); $key = "Yellow"; ##################################### ###try backup firewall server ### ##################################### $availablefirewall = "$backupfirewall"; $command = "/usr/sbin/ping $availablefirewall"; $pingfirewall = `$command`; unless ($pingfirewall =~ /alive/) { push @errors,"\n!!! Unable to reach either firewall !! +!\n"; $key = "RED"; Notify ($key, $script, @errors, @message); exit(1); exit(1); } push @errors,"\nBackup firewall $backupfirewall reached\n"; Notify ($key, $script, @errors, @message); } ############################################# ###if the one of the firewalls is ### ###available then ping the external server### ############################################# my $t=new Net::Telnet (Timeout => 30, Prompt => '/Destination> /'); $t->open("$availablefirewall"); $t->waitfor('/Destination>/'); my $anothercommand = "!ping $externalhost"; my @lines = $t->cmd("$anothercommand"); my $lines="@lines"; if ($lines =~ /alive/) { @message = "Ping of $availablefirewall successful\nPing of $externalho +st successful\n$lines\n"; $key = "Green"; ################################################## ###to verify that pingtest is testing connectivity ###uncomment the following line. You will get ###notification of successes. Leave commented if ###you do not want notification of successes. ################################################## Notify ($key, $script, @message, @errors); } else { push @message,"Cannot ping $externalhost\n$lines\n"; $key = "RED"; Notify ($key, $script, @errors, @message); } close MAIL; my @returnvalues = ($key, $availablefirewall); print "@returnvalues"; sub Notify { ################################################ ###uses three inputs. typically script name### ###and the command or part of the script that### ###failed and a key value (severity) ### ################################################ my $notifylist = "your.name@yourcompany.com"; my $key = shift @_; my $script = shift @_; my $errors = "@_"; open (MAIL, "|mail $notifylist"); if ($key eq "Red") { open (MAIL, "|mail $notifylist"); print MAIL "SUBJECT:\[!!!\] $key failure of $script\n"; print MAIL "$key failure of $script: $errors\n"; close MAIL; } if ($key eq "Yellow") { open (MAIL, "|mail $notifylist"); print MAIL "SUBJECT:\[!!\] $key failure of $script\n"; print MAIL "$errors\n"; close MAIL; } if ($key eq "Green") { open (MAIL, "|mail $notifylist"); } }

Replies are listed 'Best First'.
Re: WhichHost AKA pingtest.pl
by grinder (Bishop) on Apr 17, 2002 at 20:04 UTC
    Here are some suggestions.

    You are not testing the success of your open calls, and as they are in fact pipes, they are, in my experience, more prone to failure than regular files. To make matters worse, you are relying (hoping) that a program named 'mail' in somewhere on your PATH when the script is run. (But which one)? Not to mention trickier things like Net::Telnet objects.

    Your firewalls could roll belly up, and you wouldn't even know about it... Always always always check the results of system calls.

    Other minor issue: you should initialise an array with my @errors = (), not with my @errors = "". That said, it is a laudable practice to explicitly initialise variables. I find things are a lot clearer when spelt out:

    my $widget = undef; while( <> ) { if( useable($_) ) { $widget = $_; last; } } if( defined $widget ) { ... }

    The undef and subsequent defined form a pair. It makes it easy to see that the purpose of the loop is to get $widget out of the undefined state. Hmm, but I digress :)

    Don't do things like $availablefirewall = "$backupfirewall". Omit needless string interpolation.

    It's poor practice to munge arrays passed to subroutines. You call Notify() so:

    Notify ($key, $script, @errors, @message);
    And retrieve the parameters in Notify() thusly:
    my $key = shift @_; my $script = shift @_; my $errors = "@_";

    Someone else reading this code may puzzle for a while wondering where the fourth parameter disappeared to. Merge the arrays explicitly before passing them, or pass them by reference. Also, when you my $errors = "@_"; it's good practice to precede it with a local $" = ' '; It's a habit that will prevent you from shooting yourself in the foot in the future.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
      Thanks for the feedback.

      I have been dabbling in Perl scripting for some time now,
      but pingtest.pl is the most complete tool that I've had the
      opportunity to work on so far.

      Pingtest.pl v2.0 will be much improved, using your suggestions.

      Thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://159873]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-18 05:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found