#!/usr/bin/perl -w
use strict;
use Net::Ping ;
use Time::TimeStamp ;
my $t = Time::Timestamp->new(ts => time,time_zone => 'local');
$| = 1 ;
my $WaitIfOk = 30 ;
my $WaitIfNotOk = 10 ;
my $MaxOkCount = 25 ;
my $Count = 0 ;
sub main ()
{
my $ip = &GetDefGatewayIp ;
print <<"EOF" ;
==============================================================
Ping Response of Gateway : [$ip]
==============================================================
EOF
printf "%22s\t%7s\t%15s\n","TIME STAMP","REPLY","GATEWAY IP" ;
&PingDefGateway("$ip") ;
}
main ;
sub GetDefGatewayIp ()
{
my $_IP = "";
chomp ( my @GatewayIp = `ipconfig | findstr /i gateway` ) ;
foreach my $ip ( @GatewayIp )
{
if ( $ip =~ /^.*:\s*(\d+\.\d+\.\d+\.\d+)/ )
{ $_IP = $1 ; }
}
return $_IP ;
}
sub PingDefGateway($)
{
my $_gatewayIp = $_[0] ;
my $p = Net::Ping->new();
if ( $p->ping($_gatewayIp) )
{
&LogThis("Okay","$_gatewayIp","1") ;
$Count++ ;
sleep $WaitIfOk ;
} else
{
&LogThis("Not Ok","$_gatewayIp","1") ;
sleep $WaitIfNotOk ;
}
$p->close();
if ( $Count <= $MaxOkCount )
{
&PingDefGateway("$_gatewayIp") ;
} else
{
print GATEWAY "Exiting as server is giving steady resonse\n" ;
close GATEWAY ;
exit 0 ;
}
}
sub LogThis ($$)
{
my $_Msg = $_[0] ;
my $_IP = $_[1] ;
my $_TSFLAG = $_[2] ;
my $TimeStamp = $t->tsIso() ;
### HERE I WANT IT TO PRINT IT TO FILE like open file and just print
($_TSFLAG)? printf "%22s\t%7s\t%15s\n",$TimeStamp,$_Msg,$_IP:printf "%22s\t%7s\t%15s\n"," ",$_Msg,$_IP ;
return 0 ;
}