dascope has asked for the wisdom of the Perl Monks concerning the following question:

The following is my script. You basically run the script and specify two arguments when you run the script on the command line, a hostname (which would be your backup mail server) and the port (which is 25 for SMTP). This script flushes the queue if any. The only problem (which isn't really a problem) is the output that it prints to the screen. It prints the "220 WELCOME TO SMTP SERVER" line twice when it should only do it once. Anybody have any ideas/suggestions?
----------------------------------------------------------- #!/usr/bin/perl -w use strict; use IO::Socket; my ($host, $port, $handle, $byte, $test); unless (@ARGV == 2) { die "usage: $0 host port" } ($host, $port) = @ARGV; $handle = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $host, PeerPort => $port) or die "can't connect to port $port on $host: $!"; print "[Connected to $host:$port]\n"; print $handle "ETRN mydomain.com\nQUIT\n"; while (sysread($handle, $byte, 1) == 1) { print STDOUT $byte; } close $handle; -----------------------------------------------------------
It returns the following output:
[bashprompt] perl flush-queue.pl mail.server.com 25 [Connected to mail.server.com:25] 220 mail.server.com ESMTP (Mail Server); Wed, 4 Sep 2002 09:57:41 -040 +0 (EDT) 250 2.0.0 Queuing for node mydomain.com started 221 2.0.0 mail.server.com closing connection 220 mail.server.com ESMTP (Mail Server); Wed, 4 Sep 2002 09:57:41 -040 +0 (EDT) [bashprompt]

Replies are listed 'Best First'.
Re: Socket output issue...
by dascope (Initiate) on Sep 04, 2002 at 14:15 UTC
    I tried this and it doesn't change a thing, I run into the same problem... Argh!
    #!/usr/bin/perl -w use strict; use IO::Socket; my ($host, $port, $handle, $byte); unless (@ARGV == 2) { die "usage: $0 host port" } ($host, $port) = @ARGV; $handle = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $host, PeerPort => $port); unless ($handle) { die "cannot connect to mail server on $host" } $handle->autoflush(1); print "[Connected to $host:$port]\n"; print $handle "ETRN mydomain.com\nQUIT\n"; while ( <$handle> ) { print } close $handle;
       I just jumped in on this thread, but I ran the above code
      on my localhost, and it connects. So you have sendmail going
      on port 25, and is that the port you are trying to connect to?
      
      Output:
      zentara@zentara:~/perlplay > ./z9b localhost 25
      Connected to localhost:25
      220 zentara.zentara.net ESMTP Sendmail 8.11.3/8.11.3/SuSE 
      Linux 8.11.1-0.5; Wed, 4 Sep 2002 12:20:34 -0400
      250 2.0.0 Queuing for node mydomain.com started
      221 2.0.0 zentara.zentara.net closing connection