Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error checking with Net::FTP module

Status
Not open for further replies.

netman4u

Technical User
Mar 16, 2005
176
US
I am using the standard Net::FTP code in a subroutine as in below:

Code:
sub GetFTP
{
	##########################################################################################
	#
	#  This subroutine performs an FTP Get using the Net::FTP module
	#
	##########################################################################################
    my $ftp_server	= shift(@_);
	my $sub_pass	= shift(@_);
	my $sub_uid		= shift(@_);
	my $sub_path	= shift(@_);
	my $sub_log		= shift(@_);
	print "Beginning FTP Get from $ftp_server. Getting log $sub_log from path $sub_path\n";
	print "FTP Debug output:\n\n";
	my $ftp = Net::FTP->new("$ftp_server", Debug => 1, Timeout => 1300)
      or ErrorOut("Cannot connect to $ftp_server: $@");
    $ftp->login($sub_uid,$sub_pass)
      or ErrorOut("Cannot login ", $ftp->message);
    $ftp->cwd($sub_path)
      or ErrorOut("Cannot change working directory ", $ftp->message);
	$ftp->get($sub_log)
		or ErrorOut("get failed ", $ftp->message);
    $ftp->quit;
	print "\nEnd FTP debug output\n\n";
	print "FTP Get successful from server $ftp_server\n";
} # End sub NetFTP

What I am looking for is a way to determine after the FTP is performed if it was succsesful, and if not print the reason. This is to replace the line:

Code:
print "FTP Get successful from server $ftp_server\n";

which print regardless if the FTP works or not.

Thanks,

Nick

If at first you don't succeed, don't try skydiving.
 
if ($ftp->get($sub_log)) {
print "Success!!\n";
}



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thanks travs but it did not work:

Code:
sub GetFTP
{
	##########################################################################################
	#
	#  This subroutine performs an FTP Get using the Net::FTP module
	#
	##########################################################################################
    my $ftp_server	= shift(@_);
	my $sub_pass	= shift(@_);
	my $sub_uid		= shift(@_);
	my $sub_path	= shift(@_);
	my $sub_log		= shift(@_);
	print "Beginning FTP Get from $ftp_server. Getting log $sub_log from path $sub_path\n";
	print "FTP Debug output:\n\n";
	my $ftp = Net::FTP->new("$ftp_server", Debug => 1, Timeout => 1300)
      or ErrorOut("Cannot connect to $ftp_server: $@");
    $ftp->login($sub_uid,$sub_pass)
      or ErrorOut("Cannot login ", $ftp->message);
    $ftp->cwd($sub_path)
      or ErrorOut("Cannot change working directory ", $ftp->message);
	$ftp->get($sub_log)
		or ErrorOut("get failed ", $ftp->message);
    $ftp->quit;
	print "\nEnd FTP debug output\n\n";
	if ($ftp->get($sub_log)) {
		print "FTP Get successful from server $ftp_server\n";
	}else{
		print "FTP Get Failed\n";
	}

} # End sub GetFTP

Output:

Code:
FTP Debug output:

Net::FTP>>> Net::FTP(2.77)
Net::FTP>>>   Exporter(5.57)
Net::FTP>>>   Net::Cmd(2.29)
Net::FTP>>>   IO::Socket::INET(1.27)
Net::FTP>>>     IO::Socket(1.28)
Net::FTP>>>       IO::Handle(1.23)
Net::FTP=GLOB(0x403e3284)<<< 220 mmeweb4 FTP server ready.
Net::FTP=GLOB(0x403e3284)>>> USER weblogs
Net::FTP=GLOB(0x403e3284)<<< 331 Password required for weblogs.
Net::FTP=GLOB(0x403e3284)>>> PASS ....
Net::FTP=GLOB(0x403e3284)<<< 230 User weblogs logged in.
Net::FTP=GLOB(0x403e3284)>>> CWD /export/home/weblogs/fieldnet/
Net::FTP=GLOB(0x403e3284)<<< 250 CWD command successful.
Net::FTP=GLOB(0x403e3284)>>> PORT 170,6,240,108,249,120
Net::FTP=GLOB(0x403e3284)<<< 200 PORT command successful.
Net::FTP=GLOB(0x403e3284)>>> RETR fieldnet.24311.access.mmeweb4.log
Net::FTP=GLOB(0x403e3284)<<< 150 Opening ASCII mode data connection for fieldnet.24311.access.mmeweb4.log (99001413 bytes).
Net::FTP=GLOB(0x403e3284)<<< 226 Transfer complete.
Net::FTP=GLOB(0x403e3284)>>> QUIT
Net::FTP=GLOB(0x403e3284)<<< 221-You have transferred 99341454 bytes in 1 files.
Net::FTP=GLOB(0x403e3284)<<< 221-Total traffic for this session was 99341903 bytes in 1 transfers.
Net::FTP=GLOB(0x403e3284)<<< 221-Thank you for using the FTP service on mmeweb4.
Net::FTP=GLOB(0x403e3284)<<< 221 Goodbye.

End FTP debug output

FTP Get Failed

If at first you don't succeed, don't try skydiving.
 
Code:
 $ftp->get($sub_log)
        or ErrorOut("get failed ", $ftp->message);
    $ftp->quit;
    print "\nEnd FTP debug output\n\n";
    if ($ftp->get($sub_log)) {
        print "FTP Get successful from server $ftp_server\n";
    }else{
        print "FTP Get Failed\n";
    }
You do a get, the quit, then try to get again after the session is closed.
get rid of the first get and move the quit below the second get.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Travs it looks like you just cut and pasted code from my post that I stated does not work.

No help there!

If at first you don't succeed, don't try skydiving.
 
Did you bother to read the message below the code? I only posted your code so you could see where the problem was at.

You do a get, the quit, then try to get again after the session is closed.

Get rid of the first get and move the quit below the second get.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
netman4u,

Travs was dead on in his diagnosis of the latest code you provided. It's all there in red:

Code:
sub GetFTP {
	#  This subroutine performs an FTP Get using the Net::FTP module

	my $ftp_server = shift;
	my $sub_pass   = shift;
	my $sub_uid    = shift;
	my $sub_path   = shift;
	my $sub_log    = shift;
	
	print "Beginning FTP Get from $ftp_server. Getting log $sub_log from path $sub_path\n";
	print "FTP Debug output:\n\n";
	
	my $ftp = Net::FTP->new("$ftp_server", Debug => 1, Timeout => 1300)
		or ErrorOut("Cannot connect to $ftp_server: $@");
	
	$ftp->login($sub_uid,$sub_pass)
		or ErrorOut("Cannot login ", $ftp->message);
	$ftp->cwd($sub_path)
		or ErrorOut("Cannot change working directory ", $ftp->message);
	$ftp->get($sub_log)
		or ErrorOut("get failed ", $ftp->message);
	
	[COLOR=red]$ftp->quit;[/color]
	print "\nEnd FTP debug output\n\n";
	
	if ([COLOR=red]$ftp->get($sub_log)[/color]) {
		print "FTP Get successful from server $ftp_server\n";
	} else {
		print "FTP Get Failed\n";
	}
} # End sub GetFTP

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top