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

I've been at this for ages, and I just can't seem to get it to work.

I am attempting to retrieve the EGLL.TXT file via FTP using Perl, and put it into a specific folder on my server. I can connect to the server, I can even see the file (ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/EGLL.TXT) online, but I can't get it to pull onto my server when I run the script.

The code:
# Log into the ftp # my $ftp; $ftp = Net::FTP->new("tgftp.nws.noaa.gov"); $ftp->login("anonymous", "anonymous") or die "login error"; $ftp->cwd("data/observations/metar/stations") or die "cwd failed"; $ftp->ascii; $ftp->get("$station_name.TXT", "../area/weather/$station_name.TXT") or + die "failed to retrieve file $station_name.TXT"; $ftp->quit; # End ftp session #
The error:
./get_weather.pl EGLL failed to retrieve file EGLL.TXT at ./get_weather.pl line 31.

Any insight at all is going to rock my world. Thank you in advance.

Replies are listed 'Best First'.
Re: Perl FTP troubles
by marto (Cardinal) on Apr 16, 2016 at 09:17 UTC

    Find out why it failed:

    $ftp->get("$station_name.TXT", "../area/weather/$station_name.TXT") or die "failed to retrieve file $station_name.TXT - $ftp->message";

    Consider turning on debugging:

    my $ftp = Net::FTP->new("tgftp.nws.noaa.gov", DEBUG => 1);

    Update: here's a basic working example:

    #!/usr/bin/perl use strict; use warnings; use Net::FTP; # Log into the ftp # my $station_name = 'EGLL.TXT'; my $ftp = Net::FTP->new("tgftp.nws.noaa.gov", DEBUG => 1); $ftp->login("anonymous", "anonymous") or die "login error - " , $ftp->message; $ftp->cwd("data/observations/metar/stations") or die "cwd failed - ", $ftp->message; $ftp->ascii; # N.B. I changed the destination path here: $ftp->get("$station_name", "d:\\junk\\$station_name") or die "failed to retrieve file $station_name - ", $ftp->message; $ftp->quit; # End ftp session #

    Update: fixed typo

      Progress! It's at least a little different, now.

      Error:

      failed to retrieve file EGLL.TXT - Permission denied.

      In your working example, were you able to get the file? I copypasted it, and only changed the destination path.

        Yes:

        D:\junk>type EGLL.TXT 2016/04/16 10:20 EGLL 161020Z AUTO 34008KT 300V040 9999 -RA BKN011/// OVC023/// //////T +CU 06/02 Q 1004 TEMPO RA

        Do you have appropriate permissions to create a file in the target path? I've also tested the following successfully:

        $ftp->get("$station_name", "..\\$station_name") or die "failed to retrieve file $station_name - ", $ftp->message;