Operating System - HP-UX
1832865 Members
2732 Online
110048 Solutions
New Discussion

Re: need a ping perl script looking for the Redirect Host (New nexthop)

 
SOLVED
Go to solution
'chris'
Super Advisor

need a ping perl script looking for the Redirect Host (New nexthop)

hi everyone

I need a perl script to send a mail if the Redirect Host (New nexthop)
will be different than 192.168.0.15

# ping 192.168.24.1
PING 192.168.24.1 (192.168.24.1) 56(84) bytes of data.
From 192.168.0.10: icmp_seq=1 Redirect Host(New nexthop: 192.168.0.15)
64 bytes from 192.168.24.1: icmp_seq=1 ttl=255 time=35.8 ms
64 bytes from 192.168.24.1: icmp_seq=2 ttl=255 time=37.7 ms
64 bytes from 192.168.24.1: icmp_seq=3 ttl=255 time=34.7 ms
64 bytes from 192.168.24.1: icmp_seq=4 ttl=255 time=36.2 ms

kind regards
chris
8 REPLIES 8
RAC_1
Honored Contributor
Solution

Re: need a ping perl script looking for the Redirect Host (New nexthop)

Why perl?? Shell script will do just fine.

one=$(ping xyz -n3|grep -i 'New nexthop' |awk -F : '{print $2}')
if [ ${one} ~= "192.168.0.15" ];then
echo "Nexthop is not \"192.168.0.15\""
else
echo "Nexthop is \"192.168.0.15\""
fi

There is no substitute to HARDWORK
Hein van den Heuvel
Honored Contributor

Re: need a ping perl script looking for the Redirect Host (New nexthop)


Nitpicking.... but being a performance nutcase I can not stand activating two images where one is made to do the job.
Here grep + awk where awk is just made to select.

--- old ---
one=$(ping xyz -n3|grep -i 'New nexthop' |awk -F : '{print $2}')
--- suggested ---
one=$(ping xyz -n3| awk -F : '/New nexthop/ {print $2}')

of course both methods would have to deal with the ")" which is also picked up in $2.


In perl that would be, just picking up the address (untested)

nexthop=$(ping xyz -n3 | perl -ne 'print $1 if /hop:\s+([0-9.]+)/')

Of course you could also split (':')

But then in perl I would probably also do the ping and mail from the same script.

(again untested and unfinished...)

$good_next_hop = "192.168.0.15";
foreach (`ping xyz -n3`) {
next_hop = $1 if /hop:\s+([0-9.]+)/;
}
if ($next_hop .ne. $good_next_hop) {
mailx...
}

Cheers,
Hein.

'chris'
Super Advisor

Re: need a ping perl script looking for the Redirect Host (New nexthop)

thanks a lot, but still get errors:
-------------------------------------------------------------------------------------------------------------
#!/usr/bin/perl -w

use strict;
use warnings;

my $good_next_hop = "192.168.0.15";

foreach (`ping 192.168.24.1 -n4`) {

next_hop = $1 if /hop:\s+([0-9.]+)/;

}

if ($next_hop .ne. $good_next_hop){

'echo "Message" | mailx -s "primary firewall is down" postmaster@domain.net'

}
-------------------------------------------------------------------------------------------------------------

# perl check.cgi
Can't modify constant item in scalar assignment at check.cgi line 10, near "$1 if"
Global symbol "$next_hop" requires explicit package name at check.cgi line 14.
syntax error at check.cgi line 14, near ".ne"
Execution of check.cgi aborted due to compilation errors.


James R. Ferguson
Acclaimed Contributor

Re: need a ping perl script looking for the Redirect Host (New nexthop)

Hi Chris:

Try this version of Hein's script:

#!/usr/bin/perl
use strict;
use warnings;

my $next_hop = "127.0.0.1"; #...in case nothing matches later
my $good_next_hop = "192.168.0.15";

foreach (`ping 192.168.0.15 -n4`) {
$next_hop = $1 if /hop:\s+([0-9.]+)/;
}
if ($next_hop ne $good_next_hop) {
`echo "Message" | mailx -s "primary firewall is down" postmaster@domain.net`
}

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: need a ping perl script looking for the Redirect Host (New nexthop)

Chris:

Sorry, the 'foreach' loop had the wrong address:

#!/usr/bin/perl
use strict;
use warnings;

my $next_hop = "127.0.0.1"; #...in case nothing matches later
my $good_next_hop = "192.168.0.15";

foreach (`ping 192.168.24.1 -n4`) {
$next_hop = $1 if /hop:\s+([0-9.]+)/;
}
if ($next_hop ne $good_next_hop) {
`echo "Message" | mailx -s "primary firewall is down" root`
}

Regards!

...JRF...
'chris'
Super Advisor

Re: need a ping perl script looking for the Redirect Host (New nexthop)

thanks a lot

this script seems to work without errors:

#!/usr/bin/perl -w
use strict;
use warnings;

my $mail = "postmaster\@domain.net";
my $next_hop = "127.0.0.1"; #...in case nothing matches later
my $good_next_hop = "192.168.0.15";

foreach (`ping 192.168.24.1 -c 4`) {
$next_hop = $1 if /hop:\s+([0-9.]+)/;
}
if ($next_hop ne $good_next_hop) {
`echo "primary firewall is down" | mailx -s "primary firewall is down" $mail`
}
exit;


but I have another big problem
if I do a ping for a first time I get following:

first ping:

# ping 192.168.24.1 -c 4

PING 192.168.24.1 (192.168.24.1) 56(84) bytes of data.
From 192.168.0.10: icmp_seq=1 Redirect Host(New nexthop: 192.168.0.15)
64 bytes from 192.168.24.1: icmp_seq=1 ttl=255 time=37.8 ms
64 bytes from 192.168.24.1: icmp_seq=2 ttl=255 time=33.6 ms
64 bytes from 192.168.24.1: icmp_seq=3 ttl=255 time=35.1 ms
64 bytes from 192.168.24.1: icmp_seq=4 ttl=255 time=37.8 ms

--- 192.168.24.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 33.658/324.144/37.895/1.831 ms


second ping:

# ping 192.168.24.1 -c 4

PING 192.168.24.1 (192.168.24.1) 56(84) bytes of data.
64 bytes from 192.168.24.1: icmp_seq=1 ttl=255 time=34.7 ms
64 bytes from 192.168.24.1: icmp_seq=2 ttl=255 time=36.2 ms
64 bytes from 192.168.24.1: icmp_seq=3 ttl=255 time=37.1 ms
64 bytes from 192.168.24.1: icmp_seq=4 ttl=255 time=34.3 ms

--- 192.168.24.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 34.393/35.641/37.153/1.152 ms


this script will be scheduled every half hour
first ping looks for the next hop but following next one next it doesn't.
now I will get many wrong mails.
knows someone any ping command to look every time for the next hop ?
'chris'
Super Advisor

Re: need a ping perl script looking for the Redirect Host (New nexthop)

# ip route flush cache

seems to solve my problem, but howto put this command before ping to this perl program ?
James R. Ferguson
Acclaimed Contributor

Re: need a ping perl script looking for the Redirect Host (New nexthop)

Hi Chris:

You can run an external command in perl serveral ways. In this case, probably the easiest way is to enclose it in backticks where you want to place it. End the line with a semicolon, of course.

...
`ip route flush cache`;
foreach (`ping 192.168.24.1 -c 4`) {

Regards!

...JRF...