Solved

PERL Script results in error

Posted on 2009-04-09
6
411 Views
Last Modified: 2012-05-06
Hello-

I am trying to run a script that logs into a pop3 server multiple times using a text file with usernames and passwords. The purpose of this is to migrate user accounts from an IMAIL server to a MailEnable server via an IMAP connector.

urgoll has provided the script below, but when it is run I get an error that says:
Bad file descriptor at script.txt line 24, <FILE> line 2

C:\>perl script.txt input.txt
Processing user philip1 with password password1
   Account has 27 messages.
Processing user philip2 with password password2
Could not log in: Bad file descriptor at script.txt line 24, <FILE> line 2.

I have just installed the most recent version of ActivePerl on WIndows XP.

The input file looks like this:

philip1 password1
philip2 password2
philip3 password3
...

Any thoughts on how to resolve this issue?

THank you,
#!/usr/bin/perl

 

use strict;

use Net::POP3;

 

# Replace following line with the actual server name for the POP3 service

my $servername = 'rt.accovia.com';

 

my $filename = shift;

if (!$filename || $filename eq '') { 

	print "Usage: script.pl <inputfilename>\n",

	      "   where <inputfilename> is a text file with username and passwords for the POP accounts.\n\n";

	exit(1);

}

 

 

my $pop = Net::POP3->new($servername, Timeout => 120);	# define server with 2 minutes timeout

 

open (FILE,$filename) or die "Cannot open input file";

 

while (<FILE>) {

	if ($_ =~ /^\s*(\S+)\s+(\S+).*/ ) {

		print "Processing user ",$1," with password ",$2,"\n";

		$pop->login($1,$2) or die "Could not log in: $!";

		my $msgnums = $pop->list or die "Could not get msg list: $!";

		my $cnt = keys %$msgnums || 0;

		print "   Account has $cnt messages.\n";

		$pop->reset();

		$pop->quit();

	} else {

		print "Line didn't match expected values:\n";

		print $_;

	}

}

close(FILE);

print "Done processing input file\n";

Open in new window

0
Comment
Question by:rckit2000
6 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24109233
Try turning debugging on.
Usually that error happens when the socket was closed.

Try:
my $pop = Net::POP3->new($servername, Timeout => 120, Debug=> 1);

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24109323
try also moving the  $pop->quit(); out of the loop
0
 

Author Comment

by:rckit2000
ID: 24109355
Thanks - I turned on debug as you suggested and this is the result: Also, I switched the accounts around in the input file adn got the identical result..

C:\>perl script.txt input2.txt
Net::POP3>>> Net::POP3(2.29)
Net::POP3>>>   Net::Cmd(2.29)
Net::POP3>>>     Exporter(5.62)
Net::POP3>>>   IO::Socket::INET(1.31)
Net::POP3>>>     IO::Socket(1.30_01)
Net::POP3>>>       IO::Handle(1.27)
Net::POP3=GLOB(0x19b5554)<<< +OK X1 Greetings!  
Processing user philip1 with password XXXXXX
Net::POP3=GLOB(0x19b5554)>>> USER philip1
Net::POP3=GLOB(0x19b5554)<<< +OK send your password
Net::POP3=GLOB(0x19b5554)>>> PASS ....
Net::POP3=GLOB(0x19b5554)<<< +OK maildrop locked and ready
Net::POP3=GLOB(0x19b5554)>>> STAT
Net::POP3=GLOB(0x19b5554)<<< +OK 0 0
Net::POP3=GLOB(0x19b5554)>>> LIST
Net::POP3=GLOB(0x19b5554)<<< +OK 0 messages (0 octets)
   Account has 0 messages.
Net::POP3=GLOB(0x19b5554)>>> RSET
Net::POP3=GLOB(0x19b5554)<<< +OK 0
Net::POP3=GLOB(0x19b5554)>>> QUIT
Net::POP3=GLOB(0x19b5554)<<< +OK POP3 Server saying Good-Bye
Processing user philip2 with password XXXXX
Could not log in: Bad file descriptor at script.txt line 24, <FILE> line 2.
0
Want to promote your upcoming event?

Attending an event? Speaking at a conference? Or exhibiting at a tradeshow? Easily inform your contacts by using a promotional banner in your email signature. This will ensure your organization’s most important contacts are in the know.

 

Author Comment

by:rckit2000
ID: 24109396
I removed the quit statement and got the following result after teh first account was successful the second login failed:

Processing user philip2 with password XXXXX
Net::POP3=GLOB(0x19b4f5c)>>> USER philip1
Net::POP3=GLOB(0x19b4f5c)<<< -ERR Not in authorization state
Could not log in:  at script3.txt line 24, <FILE> line 2.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 24109488

 

open (FILE,$filename) or die "Cannot open input file";

 

while (<FILE>) {

    if ($_ =~ /^\s*(\S+)\s+(\S+).*/ ) {

        print "Processing user ",$1," with password ",$2,"\n";

        my $pop = Net::POP3->new($servername, Timeout => 120);

        $pop->login($1,$2) or die "Could not log in: $!";

        my $msgnums = $pop->list or die "Could not get msg list: $!";

        my $cnt = keys %$msgnums || 0;

        print "   Account has $cnt messages.\n";

        $pop->reset();

        $pop->quit();

    }

    else {

        print "Line didn't match expected values:\n";

        print $_;

    }

}

close(FILE);

Open in new window

0
 

Author Closing Comment

by:rckit2000
ID: 31568703
Bravo!

Thank you very much!!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The Microsoft Exchange server database may become damaged or corrupted due to many reasons, such as system failure. When this happens, it is usually urgent to restore the system to a stable and working state. There are different symptoms and causes …
This short article will present "How to import ICS Calendar onto Office 365 Calendar". I was searching for free (or not free) tools to convert ICS to CSV without success. The only tools I found & working well were online tools...this was too hard to…
In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now