#!/usr/bin/perl -w
# Check the mail server status
# Author Mars Huang
use strict;
use Socket;
use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep
clock_gettime clock_getres clock_nanosleep clock
stat );
if($#ARGV != 1)
{
print "Usage: programe userName password\n";
exit;
}
my $userName=shift;
my $password=shift;
my $PF_INET=2;
my $SOCK_DGRAM=1;
my $port = 110;
my $proto = getprotobyname("tcp");
my $address = pack('SnC4x8',$PF_INET,$port,218,204,249,6);
my $buffer;
my $errorMessage;
my($startTime,$startMs,$endTime,$endMs,$spentTime);
my $success = 0;
socket(SOCKET,$PF_INET,$SOCK_DGRAM,$proto) or die "Can't build a socket";
# Begin test
($startTime,$startMs) = gettimeofday;
connect(SOCKET,$address) or die "Can't connect to server";
$buffer=;
if($buffer !~ /^\+OK/)
{
print "ERROR protocol error during welcome";
}
# Check the username
send(SOCKET,"user $userName\r\n",0);
$buffer=;
if($buffer !~ /^\+[oOkK]/)
{
print "ERROR useraname ";
exit;
}
# Check the password
send(SOCKET,"pass $password\r\n",0);
$buffer=;
if($buffer !~ /^\+[oOkK]/)
{
print "ERROR password error \n";
exit;
}
# Check the list
send(SOCKET,"list\r\n",0);
$buffer=;
if($buffer !~ /^\+[oOkK]/)
{
print "ERROR list\n";
exit;
}
# Read the mail
send(SOCKET,"retr 1\r\n",0);
while()
{
if(m/\-ERR/)
{
$errorMessage = $_;
print "ERROR: ",$errorMessage;
exit;
}
if(m/^\.\r\n$/)
{
$success =1;
last;
}
}
# Send the quit
send(SOCKET,"quit\r\n",0);
$buffer=;
if($buffer !~ /^\+OK/)
{
print "ERROR: ",$buffer;
exit;
}
($endTime,$endMs) = gettimeofday;
$spentTime = ($endTime - $startTime) + ($endMs - $startMs) / 1000000;
if($success == 1)
{
printf("OK ! Time used : %.3f sec \n", $spentTime);
}
else
{
print "ERROR at retr. \n";
}
阅读(1757) | 评论(0) | 转发(1) |