Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Net::FTP Woes

by chaoticset (Chaplain)
on Feb 03, 2003 at 20:54 UTC ( [id://232393]=perlquestion: print w/replies, xml ) Need Help??

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

I've been banging my head against this very, very simple code for a few days and against a few different servers. What on earth am I doing wrong here?
my $host = 'ftp.ftphost.com'; my $u = 'username'; my $p = 'password'; my $ftp; unless ($ftp = Net::FTP->new($host)) { die "No FTP for you!\n\n"; } $ftp->login($u, $p); $ftp->binary; print $ftp->dir("\\");
This is on a WinME machine, perl 5.6.1 with Net::FTP installed using the CPAN module. I'm really curious, because this is (roughly) the example given in the Net::FTP tutorial elsewhere (minus host-specific info and minus a few method calls on the $ftp object).

-----------------------
You are what you think.

Replies are listed 'Best First'.
Re: Net::FTP Woes
by steves (Curate) on Feb 03, 2003 at 22:38 UTC

    What's printed? dir returns a reference to an array in scalar context. Try this:

    my @lines = $ftp->dir('\\'); print join("\n", @lines), "\n";

    I might add that even on Windows FTP servers I've always used forward slashes as directory separators when in FTP. Show us your resulting output if this still fails.

      Gladly. The output was
      C:\WINDOWS\Profiles\Gregory\Desktop\LLPSV>perl rpasafe.pl C:\WINDOWS\Profiles\Gregory\Desktop\LLPSV>
      This took a minute or so.
      Here's the whole of the script I just ran (it's the pertinent bits from the rest of the script, separated to determine the problem)
      #/usr/bin/perl -w use strict; use warnings; ########################################################### # # rpasafe.pl -- robot propagator attempt # ########################################################### use Net::FTP; my $host = 'ftp.ftpserver.com'; my $u = 'user'; my $p = 'pass'; my $ftp; unless ($ftp = Net::FTP->new($host)) { die "No FTP for you!\n\n"; } $ftp->login($u, $p); $ftp->binary; my @lines = $ftp->dir('//'); print join("\n", @lines), "\n";


      -----------------------
      You are what you think.
Re: Net::FTP Woes
by steves (Curate) on Feb 03, 2003 at 23:59 UTC

    Okay, so let's add to the error checking. First, I'd do this:

    $ftp->login($u, $p) or die "Failed to login: $!\n";
    and I'd add this to catch any dir errors:
    my $lines = $ftp->dir("/"); die "Can't get directory listing: $!\n" if (!defined($lines)); print join("\n", @$lines), "\n";

    Also, it may help to set Net::FTP's Debug level to see what's going on, like this:

    my $ftp = Net::FTP->new($host, Debug => 5);

    You know there's something in the top level directory? I still wouldn't use '/' since most ftp servers will try to get you a list from the root directory on the box which may fail due to permissions. Add this code to see where you are after you log in:
    print "Current directory is ", $ftp->pwd(), "\n";
Re: Net::FTP Woes
by steves (Curate) on Feb 03, 2003 at 23:30 UTC

    Well // almost certainly is not a directory on your ftp server. The reason for the original '\\' would be that you need to escape backslashes and the escape character is backslash. So either use '/' alone or, better yet, just leave it empty and you should get the contents of the current directory (the one that you logged into).

      #/usr/bin/perl -w use strict; use warnings; ########################################################### # # rpasafe.pl -- robot propagator attempt # ########################################################### use Net::FTP; my $host = 'ftp.ftpserver.com'; my $u = 'user'; my $p = 'pass'; my $ftp; unless ($ftp = Net::FTP->new($host)) { die "No FTP for you!\n\n"; } $ftp->login($u, $p); $ftp->binary; my @lines = $ftp->dir(); print join("\n", @lines), "\n";
      Sigh. Typo upon typo -- thanks for catching the error :) Just ran the above...
      C:\WINDOWS\Profiles\Gregory\Desktop\LLPSV>perl rpasafe.pl C:\WINDOWS\Profiles\Gregory\Desktop\LLPSV>
      Again, it seems that nothing's being returned as the main directory.

      Any ideas? The FTP server definately exists and works -- I've logged in with an FTP client.

      -----------------------
      You are what you think.

        why not run it under debug and see what happens?
        my $Debug=1; unless ($ftp = Net::FTP->new($host,Debug=>$Debug)) { die "No FTP for you!\n\n"; }

        --- demerphq
        my friends call me, usually because I'm late....

        This works fine with my ftp servers. Setting the Degug level to 1 should tell you the problem (probably a login failure given your output). You can add or die "Blah" to all of your ftp method calls as they return true if they succeed and false if they fail BTW:

        use Net::FTP; my $host = 'yoursite.com'; my $u = 'user'; my $p = 'pass'; my $ftp = Net::FTP->new($host, Debug => 1) or die "No FTP for you!\n\n +"; $ftp->login($u, $p) or die "Login failed user:$u pass:$p\n"; $ftp->binary; print "The CWD is: ", $ftp->pwd(), "\n\n"; print "The contents of the dir:\n", join("\n", $ftp->dir()), "\n";

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://232393]
Approved by Enlil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-19 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found