Measure Response Time Of SMTP Connections Using Perl

Normally, to check if my server smtp connection alive, I just do telnet to port 25 from my workstation. if the smtp banner displayed, it means that the connection to the smtp server is good. I have done this for years.  😆

$ telnet smtp.example.com 25
Trying xxx.xxx.xx.xxx...
Connected to xxx.xxx.xx.x.
Escape character is '^]'.
220 smtp.example.com ESMTP Postfix
ehlo host.example.com
250-smtp.example.com
250-PIPELINING
250-SIZE 52428800
250-ETRN
250-STARTTLS
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
quit
221 2.0.0 Bye
Connection closed by foreign host.

Although many tools that are deliberately created for this purpose, still, I prefer just using telnet to port 25. I have made a simple perl script, with the intention that the things I do for years manually can be done automatically.

Modules required:


This is the perl script

#!/usr/bin/perl -w
use strict;
use warnings;
use Net::SMTP;
use Time::HiRes qw(gettimeofday);

my $start = gettimeofday();
my $smtp = Net::SMTP->new("$ARGV[0]",
                    Hello => 'my.host.com',
                    Timeout => 60) or die "Cannot connect to remote smtp: $@";
my $banner = $smtp->banner;
my $end = gettimeofday();
my $delta = ($end - $start);

printf "%s", $banner;
printf "%s takes : %.3f seconds to get connected\n", $ARGV[0],  $delta;

$smtp->quit;
exit 0;

This is an example of how the script works

$ ./smtp-check.pl aspmx.l.google.com
mx.google.com ESMTP w15si5875063ibh.43
aspmx.l.google.com takes : 1.807 seconds to get connected
$ ./smtp-check.pl h.mx.mail.yahoo.com
mta1186.mail.mud.yahoo.com ESMTP YSmtp service ready
h.mx.mail.yahoo.com takes : 1.176 seconds to get connected
$ ./smtp-check.pl mail.cloud9.net
english-breakfast.cloud9.net ESMTP Postfix
mail.cloud9.net takes : 2.622 seconds to get connected

That’s all for now.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *