Mel
Mel

Reputation: 3965

Why is my Perl script that calls FTP all of a sudden failing?

I have a script that has been running for over a year and now it is failing:

It is creating a command file:

open ( FTPFILE, ">get_list");
print FTPFILE "dir *.txt"\n";
print FTPFILE "quit\n";
close FTPFILE;

Then I run the system command:

$command = "ftp ".$Server." < get_list | grep \"\^-\" >new_list";
$code = system($command);

The logic the checks:

if ($code == 0) {

do stuff
} else {
log error
}

It is logging an error. When I print the $code variable, I am getting 256.

I used this command to parse the $? variable:

$exit_value  = $? >> 8;
$signal_num  = $? & 127;
$dumped_core = $? & 128;

print "Exit: $exit_value Sig: $signal_num Core: $dumped_core\n";

Results:

Exit: 1 Sig: 0 Core: 0

Thanks for any help/insight.

Upvotes: 1

Views: 549

Answers (1)

DVK
DVK

Reputation: 129373

Mel - you might gain a bit more information by looking at standard error output of the ftp command.

1) Does the FTP command work by hand from shell prompt?

2) If command line ftp works, capture the output (stdout and stderr) of the ftp command and print it in Perl script. For a couple of ways to do so, see perlfaq8 - How can I capture STDERR from an external command?

The two easiest apporaches are these:

my $output = `$command 2>&1`; 

my $pid = open(PH, "$command 2>&1 |"); 
while (<PH>) { print "Next line from FTP output: $_"; } 

3) As wisely noted by Snake Plissken in a comment, an alternate (and more idiomatic and possibly easier) approach is to scrap the system call to "ftp" command and instead use Net::FTP Perl module.

Upvotes: 5

Related Questions