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"); } }

In reply to WhichHost AKA pingtest.pl by cybear

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.