Chinaunix首页 | 论坛 | 博客
  • 博客访问: 75160
  • 博文数量: 6
  • 博客积分: 1507
  • 博客等级: 上尉
  • 技术积分: 70
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-30 13:32
文章分类
文章存档

2010年(1)

2008年(5)

分类:

2008-08-17 22:19:20

#!/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) |
给主人留下些什么吧!~~