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