#!/usr/bin/perl -w # pinger for AGate200 link use strict; use POSIX; use Net::Ping; use Net::Telnet; my $host = '192.168.1.1'; # other end of the link my $agate = '192.168.1.2'; # AGate to (try to) reset my $admin = 'admin@domain.org'; # someone who cares my $maxfail = 3; # his patience my $pause = 60; # secs to sleep between pings my $fail = 0; # consider it pingable apriori my $log = '/var/log/pinger.log'; # log file my $pid = fork; exit if $pid; die "Couldn't fork: $!" unless defined($pid); POSIX::setsid() or die "Can't start a new session: $!"; my $time_to_die = 0; $SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signal_handler; open (LOG, "> $log") or die "Can't open logfile: $!\n"; select(LOG); $|++; print scalar localtime, " Started succesfully\n"; my $ping = Net::Ping->new("icmp") or die "Can't create new ping: $!\n"; until ($time_to_die) { if ($ping->ping($host)) { # print scalar localtime, " Ping $host: success\n"; $fail = 0; } else { print scalar localtime, " Ping $host: fail\n"; $fail++; } if ($fail == $maxfail) { my $result = fix(); $result ||= 'success'; print scalar localtime, " Reset: $result\n"; notify($result); } if ($fail > $maxfail) { fix(); } sleep($pause); } sub signal_handler { $ping->close(); print scalar localtime, " Exiting\n"; close LOG; $time_to_die = 1; } sub notify { my $result = shift; my $minutes = ($maxfail*$pause)/60; open(MAIL, "| mail -s 'Wake up!' $admin") or die "Couldn't fork: $!\n"; print MAIL <new(Errmode => "return"); $t->open($agate); $t->waitfor('/A Choice :/'); $t->print('9'); $t->waitfor('/reenter.*$/'); $t->print(''); $t->waitfor('/Password :/'); $t->print('figatebe'); $t->waitfor('/\(y\/n\):/'); $t->print('y'); $t->close($agate); return $t->errmsg; }