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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Net::FTP

Status
Not open for further replies.

tlm153

Programmer
Jun 13, 2003
9
US
I think I understand how to use Net::FTP. I am calling a Perl script from a Unix script. If the Perl script cannont connect to the server, I think it sends back a return code to the Unix script. Does anyone know how to capture the return code in the Unix script?
 
if you say

exit 1;

in your perl script and then check the value of $? in your shell script you'll get a return code.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
If I do the "exit 1" command in Perl, then where do I put the command within the Net::FTP command?


$ftp = Net::FTP->new($ftp_server, Debug => 0) or die "Cannot connect: $@\n";

 
Why not say:

$ftp = Net::FTP->new($ftp_server, Debug => 0) or warn "Cannot connect: $@\n";

if ($? -or whatever it is in Perl- != 0){

exit 1;

}

-dj
 
Actually, just making the Perl script die will give you the correct output for your shell script.

Don't worry about my last post...

-dj
 
I tried do the following command in Perl:

$ftp = Net::FTP->new($ftp_server, Debug => 0) or die "Cannot Connect to FTP Server";

The Perl program does the die command but when I return to the Unix script, it does not seem to get the return code. I am doing:

if ($? != 0)
then
echo "FTP failed"
date +"%Y%m%d %H:%M:%S"
echo End ${Prog}
exit 1
fi

 
You should try printing out the result of $? as soon as the Perl script ends just to see what it is doing. Perhaps you may have to get creative and instead of saying:

$ftp = Net::FTP->new($ftp_server, Debug => 0) or die "Cannot Connect to FTP Server";

you can say:

$ftp = Net::FTP->new($ftp_server, Debug => 0) or exit 255;

Just a thought,

-dj
 
As you have seen in some of the previous posts, $@ is where the Net::FTP exit status is placed. This is the syntax I use:

my $ftp = Net::FTP->new
(
"someserver.com",
Timeout => 60
) or die "Could not connect to someserver: $@\n";

$ftp->login($username, $password) or die "Could not log in. $ftp->message\n";

Another thing you could do is set Debug to a none 0/undef:

$ftp = Net::FTP->new($ftp_server, Debug => 1) or die "Cannot connect: $@\n"; # This will generate more info for you.

Remember that some of your errors are going to STDOUT unless you redirect them in your program or the shell script executing it. If you need to exit immidiately use die if not use warn, the exit command will only yield a number you will have to give meaning to.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top