#!/usr/local/bin/perl # (C) H.Ichikawa,2002/03/12 # alive.pl (check if service is alive) # version 0.03 (2002/03/15) # use IO::Socket; use IO::Select; ########### user code ############ sub comment { my $msg=shift @_; # we assume $msg is not tainted! system("perl ./ircnotice.pl \"$msg\""); # print "$msg\n"; } ############ don't change after this ########## if($#ARGV<2){ print "usage: perl alive.pl ALWAYS|ERR server service\n"; exit(); } $mode=$ARGV[0]; $server=$ARGV[1]; $service=$ARGV[2]; undef $port; $convert{"imap"}=143; $convert{"pop"}=110; $convert{"smtp"}=25; $port=$convert{$service}; unless(defined $port){ comment("invalid argument: $server:$service"); print "INVALID ARGUMENT\n"; exit(); } #main $sock= IO::Socket::INET->new(PeerAddr => $server, PeerPort => $port, Proto => 'tcp', Timeout => 3); unless(defined $sock){ comment("$server:$service is down? (cannot connect)"); print "REFUSED\n"; undef $sock; exit(); } $myselect = IO::Select->new(); $myselect->add($sock); sleep(5); unless( $myselect->can_read(3) ){ comment("$server:$service is down? (response timeout)"); print "NORES\n"; $sock->close(); undef $sock; exit(); } $sock->recv($data,100); $check="ERR"; if($service eq "imap"){ $check="OK" if($data =~ m/^\*\ OK/ ) }; if($service eq "pop"){ $check="OK" if($data =~ m/^\+OK\ / ) }; if($service eq "smtp"){ $check="OK" if($data =~ m/^220\ / ) }; $bye=""; $bye="a logout\r\n" if($service eq "imap"); $bye="quit\r\n" if($service eq "pop"); $bye="quit\r\n" if($service eq "smtp"); $sock->send($bye); $sock->close(); undef $sock; if($check eq "OK"){ comment("$server:$service is OK") if($mode =~ /^always$/i); print "OK\n" }else{ comment("$server:$service is down? (error message)"); print "ERR\n" } exit(); #EOF