Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Net::Telnet and SMTP

Reply
Thread Tools

Net::Telnet and SMTP

 
 
crr
Guest
Posts: n/a
 
      06-14-2005
I originally tried this out over in comp.lang.perl.modules, but didn't
really get anywhere, so I thought I'd take a crack at it over here.

Hi all,

I'm using Net::Telnet to test an SMTP proxy product and I've run into
an issue with it.

First, I'm trying to send a sequence of commands and then log both the
commands sent and the response from the server into a log file. The
responses need to match up EXACTLY to the commands to which they are in
response. Second, I've tried Net::SMTP and it doesn't quite do what I
need. Since the product I'm testing may have to deal with......er,
impolite SMTP clients (read: crackers) I want to make sure that if the
SMTP protocol being sent is broken that the proxy reponds in a correct
(read: secure) way. Unfortunately Net::SMTP doesn't give me the level
of control I need....mostly because it's too polite.

I've included the script and a sample text file (that feeds the scripts
the commands) below. It's been since sometime in v4 since I last did
anything with PERL (unfortunately) so I apologize in advance for the
mess.

Thanks,

crr

************************************Script******** ******************************
#This script reads from a generated smtp test file and writes to a log
file in
#the logs directory, which will be created under the directory from
which the
#test is run. I recommend that you place the test file in the same dir
as the
#script, if for no other reason than that you won't have to maintain in
the
#script the location from which you're running the file.
#
#test file format note:
#The first line of the test file *must* contain the dns name (if you're
using
# DNS) or the IP address of the server under test, followed by a colon
( and
# the port to which you're connecting, typically 25
# You can place comments in the test file by prepending the line with a
#. Note
# that this can only be placed at the start of the line
# Each test should beging with "***Start test" (without quotes) and end
with
# "***Stop test"

use strict;
#no strict 'refs';
use Net::Telnet;

#set the test and log file names, and some globals
my $testfilename = "smtptests.txt";
my $t = localtime(time());
my $logfilename = "./logs/smtplog${t}.txt";
our $testcount = 0;
our $debug = 1;

open TESTFILE, $testfilename or die "Cannot open test file,
${testfilename}";

#print $logfilename;
if (-e $logfilename) {
die "Log file ${logfilename} already exists!";
}

mkdir 'logs', 0744;

open LOGFILE, ">${logfilename}" or die "Cannot open log file,
${logfilename}";
if ($debug) {print "*****Created log file\n";}

#grab the server and port out of the first line of the file
my ($server, $port) = split (/\:/, getline(*TESTFILE));

#create the telnet object and set some initial values
our $telnet = new Net::Telnet (Timeout => 660,
Telnetmode => 0,
Host => $server,
Port => $port,
Prompt => '//',
Errmode => 'die');
#need to set the prompt to null, since SMTP doesn't have a prompt,
large timeout is
#for testing our timeout

#timeout exception for debugging the script
$telnet->timeout(30) if ($debug);

if ($debug) {print "*****Telnet object created\n";}

$t = localtime(time());
print LOGFILE "Starting test of server ${server} at ${t}\n";

#begin processing the main part of the file
processfile(*TESTFILE, *LOGFILE);

close TESTFILE;
close LOGFILE;

#print "${server}";
#print $port;

#processfile is where the main work of the program is coordinated. it
reads
#commands from the test file and organizes the starting and stopping of
each
#test
sub processfile {
if ($debug) {print "*****Entering processfile\n";}
my $TF = shift;
my $LOG = shift;
my $line = getline($TF);

if ($line eq eof) {return 0;}

if ($debug) {print $line;}
while ($line ne eof) {
# checks the line and read if it's a command to start a
test
if ($line eq "***Start test\n") {
print "*****Starting test\n" if ($debug);
$testcount++;
print $LOG "Starting test ${testcount}\n";
if (defined(dotest($TF, $LOG))) {
print $LOG "Test ${testcount}
finished\n";
} else {
print $LOG "Test ${testcount}
failed\n";
}#end else
}#end if
#We should never encounter a stop test line in
processfile
if ($line eq "***Stop test\n") {die "Error in test
file!"}

$line = getline($TF);
if ($debug) {print $line;}
}#end while

return 0;

#once we've started a test, dotest actually runs the commands,
including opening
#the telnet session
sub dotest {
my $TF = shift;
my $LOG = shift;
my @response = ();

print "*****Entered dotest\n" if ($debug);

my $line = getline($TF);
$telnet->input_log($LOG);
print $LOG "Recd: ";
$telnet->open();
print "*****Telnet session open\n" if ($debug);

#Try a short delay
delay(.1);

$telnet->get(); #read output for input_log
#my $firstline = $telnet->getline();
#print $LOG "Recd: ${firstline}";

while($line ne "***Stop test\n"){
print $LOG "Sent: ${line}";
print $LOG "Recd: ";
print $LOG $telnet->cmd(String => $line);

#Try a short delay between sending command and reading output
delay(5);
$telnet->getline();

#if (substr($line, 0, 4) eq "ehlo") {
#@response = $telnet->getlines(All => "");
#} else {@response =
$telnet->getline();}

#print $LOG "Recd: @{response}";
$line = getline($TF);
}#end while
#$telnet->dump_log('') if ($debug);

$telnet->cmd(String => localtime(time())) if ($debug);
$telnet->close();

return 0;
}#end dotest

}#end processfile

#Small sub to strip out comments in the test file and exit gracefully
if we've
#reached the end of the input file
sub getline {
my $TF = shift;
my $gotline = 0;
my $line = '';

while (!($gotline)) {
$line = <$TF>;
if (eof($TF)) {
print "*****EOF reached\n" if ($debug);
print $line if ($debug);
exit;}
$gotline = 1;
my @a = split (//, $line);
if ($a[0] eq '#') {$gotline = 0;}
}#end while

#print "${line}";
return $line;

}#end getline

#small sub to insert a variable length time delay
sub delay {
my $delaytime = shift;

my $returntime = (time + $delaytime);
while(1) {
if (time >= $returntime) {return;}
}

}#end delay

************************************text
file*************************************
10.113.15.66:25
#Test comment
***Start test
ehlo test.com
mail from: b...@test.com
rcpt to: nad...@ace66.roke.com
data
To: bob
From: Bill
Subject: test
This is a test email
..
***Stop test

 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      06-14-2005
On 14 Jun 2005 11:18:34 -0700, crr wrote:

> I originally tried this out over in comp.lang.perl.modules, but didn't
> really get anywhere, so I thought I'd take a crack at it over here.
>
> Hi all,
>
> I'm using Net::Telnet to test an SMTP proxy product and I've run into
> an issue with it.
>
> First, I'm trying to send a sequence of commands and then log both the
> commands sent and the response from the server into a log file. The
> responses need to match up EXACTLY to the commands to which they are in
> response. Second, I've tried Net::SMTP and it doesn't quite do what I
> need. Since the product I'm testing may have to deal with......er,
> impolite SMTP clients (read: crackers) I want to make sure that if the
> SMTP protocol being sent is broken that the proxy reponds in a correct
> (read: secure) way. Unfortunately Net::SMTP doesn't give me the level
> of control I need....mostly because it's too polite.
>

<snip>
Hi

It isn't clear what your problem is. What issue are you having with your
first requirement and what is the smallest complete program that will
demonstrate this?

I am not particularly familiar with Net::Telnet or Net::SMTP, but I would
be tempted to use Expect.

Mark
 
Reply With Quote
 
 
 
 
Mike
Guest
Posts: n/a
 
      06-15-2005
Mark Clements wrote:

> On 14 Jun 2005 11:18:34 -0700, crr wrote:
>
>
>>I originally tried this out over in comp.lang.perl.modules, but didn't
>>really get anywhere, so I thought I'd take a crack at it over here.
>>
>>Hi all,
>>
>>I'm using Net::Telnet to test an SMTP proxy product and I've run into
>>an issue with it.
>>
>>First, I'm trying to send a sequence of commands and then log both the
>>commands sent and the response from the server into a log file. The
>>responses need to match up EXACTLY to the commands to which they are in
>>response. Second, I've tried Net::SMTP and it doesn't quite do what I
>>need. Since the product I'm testing may have to deal with......er,
>>impolite SMTP clients (read: crackers) I want to make sure that if the
>>SMTP protocol being sent is broken that the proxy reponds in a correct
>>(read: secure) way. Unfortunately Net::SMTP doesn't give me the level
>>of control I need....mostly because it's too polite.
>>

>
> <snip>
> Hi
>
> It isn't clear what your problem is. What issue are you having with your
> first requirement and what is the smallest complete program that will
> demonstrate this?
>
> I am not particularly familiar with Net::Telnet or Net::SMTP, but I would
> be tempted to use Expect.
>
> Mark



Dump_log input_log and Output_log arguments will give you text files
with all the input.. all the output and everything in order both hex and
ascii .

You should be able to see the transactions back and forth no problem..
the text logs are primarily used for debug purposes but will work nicely
for what you need.

Its all in the readme

Mike
 
Reply With Quote
 
crr
Guest
Posts: n/a
 
      06-17-2005
Sorry, it seemed clear when I wrote the message, but then I've been
staring at this problem for two days!

My main issue is that the messages returned by the server are not
making it back to be read by the client side. It seems that if I do a
completely valid smtp session, I eventually get all the messages when I
do my final $obj->get(), but I can't get them to appear in-line with
the command that generated them. I also cannot get them to appear when
I do a shortened smtp session (such as when I generate an error) even
when I do a final get() (or getline() or getlines()).

I'm not sure if I'm hitting some sort of buffer limit or what, but the
initial messages don't have this problem. Frex, I get the smtp
greeting immediately, as well as the response to the EHLO command.

Thanks in advance for the help.

crr

 
Reply With Quote
 
crr
Guest
Posts: n/a
 
      06-17-2005
Hi, thanks for the response.

I'm actually using input_log, and I still don't see the messages
returned. Like I mentioned above, I can somtimes get them, but not
until I do my final get() after a complete smtp session, but if I do
something to generate an error (such as issuing smtp commands out of
order, etc.) I cannot get the error message returned by the server,
which is real interest here anyway.

Do you know if input_log has some sort of buffer limit before it writes
to the filehandle? Or is there some reason that it's not writing
immediately, except in the cases of the smtp greeting and the EHLO
response? Even in those cases, I have to do an $obj->get() before the
responses will be logged.

Thanks again for the help,

crr

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      06-17-2005
"crr" <> wrote in news:1119027092.183211.282440
@g43g2000cwa.googlegroups.com:

> Sorry, it seemed clear when I wrote the message,


Similarly, the context may seem clear to you now, when you are writing the
message, but *please* quote the appropriate amount of context when you
post a reply so that your readers can understand you without having to
hunt for messages (i) that may never have arrived on their newsserver,
(ii) may not have arrived on their newsserver yet, (iii) may have expired
on their newsserver, or people may simply not have threading on.

Google Groups is great as an archive but makes a lousy platform for
participating in UseNet discussions. You will have to make an extra effort
and follow established UseNet practice.

For help on how to make sure you can maximize the usefulness of this group
to you, please read the posting guidelines.

Sinan

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      06-20-2005
crr wrote:
> Hi, thanks for the response.
>
> I'm actually using input_log, and I still don't see the messages
> returned. Like I mentioned above, I can somtimes get them, but not
> until I do my final get() after a complete smtp session, but if I do
> something to generate an error (such as issuing smtp commands out of
> order, etc.) I cannot get the error message returned by the server,
> which is real interest here anyway.
>
> Do you know if input_log has some sort of buffer limit before it writes
> to the filehandle? Or is there some reason that it's not writing
> immediately, except in the cases of the smtp greeting and the EHLO
> response? Even in those cases, I have to do an $obj->get() before the
> responses will be logged.
>
> Thanks again for the help,
>
> crr
>



Yeah .. you will see input in the input log.. any returned messages will
be in output log... use it and see.
 
Reply With Quote
 
crr
Guest
Posts: n/a
 
      06-20-2005
Jim Gibson wrote:

> If you don't follow these guidelines, you will reduce the probability
> of getting good answers. Many of the most knowledgable Perl experts
> here have stopped reading posts from Google users because they too
> often violate the guidelines for this group. You should consider using
> another source for Usenet groups and using a real newsreader that does
> not have these problems.


Bleh, sorry....I don't typically use Google Groups, so I wasn't aware
that it's...er, impolite....and wasn't aware how to make Google quote a
reply. Unfortunately, Google is my only choice here at work for
UseNet, so there we are. I'll take more care in the future when using
Google Groups, though hopefully I won't have to use it very often
(painful is not the word for it).

Thanks,

crr

 
Reply With Quote
 
crr
Guest
Posts: n/a
 
      06-20-2005
A. Sinan Unur wrote:
> "crr" <> wrote in news:1119027092.183211.282440
> @g43g2000cwa.googlegroups.com:
> Google Groups is great as an archive but makes a lousy platform for
> participating in UseNet discussions. You will have to make an extra effort
> and follow established UseNet practice.


Amen to *that*, and if I had any other choice, I'd be using my nice
sane newsreader at home, but alas, alack, welladay and so forth, I'm
stuck with Google here at work. I will make an effort to use Google
more politely, now that I know how to get it to do what I want.

Thanks,

crr

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      06-20-2005
"crr" <> wrote in
news: oups.com:

> A. Sinan Unur wrote:
>> "crr" <> wrote in
>> news:1119027092.183211.282440 @g43g2000cwa.googlegroups.com:
>> Google Groups is great as an archive but makes a lousy platform for
>> participating in UseNet discussions. You will have to make an extra
>> effort and follow established UseNet practice.

>
> Amen to *that*, and if I had any other choice, I'd be using my nice
> sane newsreader at home, but alas, alack, welladay and so forth, I'm
> stuck with Google here at work. I will make an effort to use Google
> more politely, now that I know how to get it to do what I want.
>
> Thanks,


No, thank you very much for understanding our concern, and doing your
share.

Sinan

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SMTP Relay from IIS SMTP Virtual Server tshad ASP .Net 1 05-05-2008 04:26 AM
Sending SMTP mail when SMTP server is unavailable matthewjbarr@gmail.com Java 3 01-24-2006 06:53 PM
smtplib.SMTP "no attributes SMTP" Copelandia Cyanescens Python 2 07-31-2004 07:24 PM
sending of mail (smtp) - connection refused - but smtp server isrunning! Alex Hunsley Python 4 06-29-2004 04:30 PM
RE: sending of mail (smtp) - connection refused - but smtp serveris running! Raaijmakers, Vincent \(GE Infrastructure\) Python 0 06-29-2004 02:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57