Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Problem with Net::FTP

by cool (Scribe)
on Jul 10, 2007 at 11:39 UTC ( #625797=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,
Pl help me understand, what is going on here!!
I just started venturing into Object Oriented Perl. I am refferring Simon Cozen 'Beginning Perl'. OO concept is explained using the script given below. For me it is not producing anything. After execution of script, it gives me doller prompt again!!
use warnings; use strict; use Net::FTP; my $ftp=Net::FTP->new("ftp.cpan.org") or die "Couldnt connect:$@\n"; $ftp->login("anonymous"); $ftp->cwd("/pub/CPAN"); $ftp->get("README.html"); $ftp->close;
After going through Net::FTP doucumentation from CPAN I found under CONSTRUCTOR heading, an argument for firewall is given under subheading FIREWALL. last line says
This kind of setup is also referred to as an ftp proxy
As my institute uses proxy so I tried script with constructor line as
my $ftp=Net::FTP->new("ftp.cpan.org","XXXX")
(XXXX-My institute's proxy)
This time I got an error msg
Couldnt connect:Net::FTP: Bad hostname '' Also in the same subheading FIREWALL, it mentions about environment variable FTP_FIREWALL. How can I see the settings of my environment variables??
Any documentation link regarding this, any pointer, suggestions, guidance is welcome and appriciated. Thank you for reading this much.

Replies are listed 'Best First'.
Re: Problem with Net::FTP
by tirwhan (Abbot) on Jul 10, 2007 at 11:51 UTC

    You should try getting an FTP connection working manually and see what requirements you need to fulfil for that. For example, in many network setups (particularly if you're in a NATed network) you need to activate passive mode in your FTP connection. The sentence

    As my institute uses proxy

    sounds a bit suspicious to me, because there are many types of proxies for the various protocols (HTTP,STMP,FTP) and you don't seem to know which one your institute is using and whether it's being used to proxy FTP connections at all. It may be entirely transparent to you as the user or a SOCKS proxy. So I would recommend using the command line "ftp" utility and trying to get a working connection with that, trying out options until you achieve a result.

    Having said that, there is a bug in the way you are trying to set the Firewall option, the line

    my $ftp=Net::FTP->new("ftp.cpan.org","XXXX")

    should be

    my $ftp=Net::FTP->new("ftp.cpan.org",Firewall => "XXXX")

    , maybe that solves your problem already.

    <private pet rant>Whyohwhy is this option called "Firewall" in Net::FTP when it clearly refers to the proxy? A proxy is not a firewall nor the other way around! The fact that both are often implemented on the same machine does not mean they are equivalent, and confusing users (who are often confused enough of their own accord) further by using wrong terminology in the module is extremely regrettable</rant>


    All dogma is stupid.
      Thanks tirwhan for explanation,
      Just wondering whether there is any way to check, whatype of proxy my institute is using?? Exactly same problem I am facing in configuring my CPAN.pm as well. There it seems to connect to ftp.cpan but then fails everytime. And one of the error messages it shows complain about user id and password. Now strange thing is, there is none. I can browse the net with any password. Also what do you mean by manual ftp??Do you mean I should try to ftp to some machine outside the campus?? or to cpan?? if cpan than shouln't I know IP address for cpan server
      For my firefox browser setting; HTTP, SSL, FTP , GOPHER are given same proxies (XXXX) (and port 3128) but SOCKS_host has been left blank. Does this gives any clue???
      Now as you understand my level of comfortability with these lingo (ftp/proxy/firewall) could you pl direct me to some online documentation. I tried a few times to get a feel of these things but often found documents hard to understand.
      I admire you patient. Thank you for all your help.
        Just wondering whether there is any way to check, whatype of proxy my institute is using?

        Yes, ask the resident system administrator :-). S/he should also be able to give you tips on how to configure your FTP connection setup.

        Also what do you mean by manual ftp?
        I meant you should try connecting with the command line "ftp" client (and I'm assuming you have that at your disposal, if you're on Windows this may not be the case, I have no idea), which allows you to set options like proxy and mode on the command line, thus allowing for easy testing of the requirements.

        For my firefox browser setting; HTTP, SSL, FTP , GOPHER are given same proxies (XXXX) (and port 3128) but SOCKS_host has been left blank. Does this gives any clue???

        This does seem to indicate that your institute runs an FTP proxy on that machine, and your code (with the change I made) should work. Have you tried turning on debugging, as rpanman suggested? If so, what's the output?

        could you pl direct me to some online documentation

        Just type "FTP firewall proxy" into Google and you'll find loads of documentation on the subject. The wikipedia entry on FTP might be another place to start


        All dogma is stupid.
      Thanks tirwhan for explanation,
      Just wondering whether there is any way to check, whatype of proxy my institute is using?? Exactly same problem I am facing in configuring my CPAN.pm as well. There it seems to connect to ftp.cpan but then fails everytime. And one of the error messages it shows complain about user id and password. Now strange thing is that I can browse the net without any password. Also please tell me what do you mean by manual ftp??Do you mean I should try to ftp to some machine outside the campus?? or to cpan?? if cpan than shouln't I know IP address for cpan server and also the file location there?
      For my firefox browser setting; HTTP, SSL, FTP , GOPHER are given same proxies (XXXX) (and port 3128) but SOCKS_host has been left blank. Does this gives any clue???
      Now as you understand my level of comfortability with these lingo (ftp/proxy/firewall) could you pl direct me to some online documentation. I tried a few times to get a feel of these things but often found documents hard to understand.
      I admire your patience. Thank you for all your help.
Re: Problem with Net::FTP
by rpanman (Scribe) on Jul 10, 2007 at 11:57 UTC
    Are you sure that this isn't working correctly (sounds stupid I know). The output should be the file README.html being transferred to the current directory. There is no printout as such from the script (except on error). Just thought it might be worth checking.

    Perhaps you could post the output of the script with debugging turned on, like this:
    use warnings; use strict; use Net::FTP; my $ftp=Net::FTP->new("ftp.cpan.org", Debug => 1) or die "Couldnt connect:$@\n"; $ftp->login("anonymous"); $ftp->cwd("/pub/CPAN"); $ftp->get("README.html"); $ftp->close;
    That may help in isolating the problem.
      Thankyou for your suggestion rpanman,
      [sjoh@nucleix method]$ perl ftp1.pl Net::FTP>>> Net::FTP(2.75) Net::FTP>>> Exporter(5.58) Net::FTP>>> Net::Cmd(2.26) Net::FTP>>> IO::Socket::INET(1.27) Net::FTP>>> IO::Socket(1.28) Net::FTP>>> IO::Handle(1.24) Net::FTP=GLOB(0x9da13a0)<<< 220 (vsFTPd 2.0.5) Net::FTP=GLOB(0x9da13a0)>>> user anonymous Net::FTP=GLOB(0x9da13a0)<<< 331 Please specify the password. Net::FTP=GLOB(0x9da13a0)>>> PASS .... Net::FTP=GLOB(0x9da13a0)<<< 230 Login successful. Net::FTP=GLOB(0x9da13a0)>>> CWD /pub/CPAN Net::FTP=GLOB(0x9da13a0)<<< 250-The Comprehensive Perl Archive Network + (http://www.cpan.org/) Net::FTP=GLOB(0x9da13a0)<<< 250-master site has been from the very beg +inning (1995) hosted at FUNET, Net::FTP=GLOB(0x9da13a0)<<< 250-the Finnish University NETwork. Net::FTP=GLOB(0x9da13a0)<<< 250- Net::FTP=GLOB(0x9da13a0)<<< 250- Net::FTP=GLOB(0x9da13a0)<<< 250 Directory successfully changed. Net::FTP=GLOB(0x9da13a0)>>> PASV Net::FTP=GLOB(0x9da13a0)<<< 227 Entering Passive Mode (193,201,200,224 +,185,150) Net::FTP=GLOB(0x9da13a0)>>> RETR README.html Net::FTP=GLOB(0x9da13a0): Timeout at ftp1.pl
      but now if I add as tirwhan suggested,
      my $ftp=Net::FTP->new("ftp.cpan.org", Firewall => "XXXX", Debug => 1) ine 8
      Where XXXX- proxy of my institute
      I am getting message
      Couldnt connect:Net::FTP: connect: Connection refused
        Net::FTP=GLOB(0x9da13a0)<<< 220 (vsFTPd 2.0.5)
        Net::FTP=GLOB(0x9da13a0)>>> user anonymous
        Net::FTP=GLOB(0x9da13a0)<<< 331 Please specify the password.

        That certainly looks like a successful FTP connection to me and

        Couldnt connect:Net::FTP: connect: Connection refused

        obviously isn't, so skip the Firewall setting. It seems to break your setup rather than fixing it.

        When you run the script without setting Firewall, is the file README.html created or not? If it is, then everything is working right (more or less) and the main problem is that it didn't print anything to tell you so. If not, then the problem is that timeout and you'll probably want to try FTPing the file using a traditional interactive FTP client to make sure that works before beating your head against this wall too much more.

        From the debug output it looks like the script is connecting using passive mode - you can see this from the line
        Net::FTP=GLOB(0x9da13a0)>>> PASV
        ... it then times out for some reason - presumably because it is not receiving any data from ftp.cpan.org (maybe this is being firewalled out.
        (more info on passive/active FTP here: http://slacksite.com/other/ftp.html)

        ... which is strange since when I run the same script it does not enter passive mode...
        Net::FTP>>> Net::FTP(2.77) Net::FTP>>> Exporter(5.58) Net::FTP>>> Net::Cmd(2.28) Net::FTP>>> IO::Socket::INET(1.31) Net::FTP>>> IO::Socket(1.30) Net::FTP>>> IO::Handle(1.27) Net::FTP=GLOB(0x22d19c)<<< 220 Welcome to the ftp.linux.hr FTP service +. Net::FTP=GLOB(0x22d19c)>>> USER anonymous Net::FTP=GLOB(0x22d19c)<<< 331 Please specify the password. Net::FTP=GLOB(0x22d19c)>>> PASS .... Net::FTP=GLOB(0x22d19c)<<< 230- Net::FTP=GLOB(0x22d19c)<<< 230-Ovo je ftp.linux.hr, FTP posluzitelj Hr +vatske Udruge Linux Korisnika. Net::FTP=GLOB(0x22d19c)<<< 230- Net::FTP=GLOB(0x22d19c)<<< 230-Pogledajte README datoteku za opis sadr +zaja na nasem posluzitelju. Net::FTP=GLOB(0x22d19c)<<< 230- Net::FTP=GLOB(0x22d19c)<<< 230-CD ISO imagei su na iso.linux.hr! Net::FTP=GLOB(0x22d19c)<<< 230- Net::FTP=GLOB(0x22d19c)<<< 230- -- ftpadmin@linux.hr Net::FTP=GLOB(0x22d19c)<<< 230 Login successful. Net::FTP=GLOB(0x22d19c)>>> CWD /pub/CPAN Net::FTP=GLOB(0x22d19c)<<< 250-The Comprehensive Perl Archive Network +(http://www.cpan.org/) Net::FTP=GLOB(0x22d19c)<<< 250-master site has been from the very begi +nning (1995) hosted at FUNET, Net::FTP=GLOB(0x22d19c)<<< 250-the Finnish University NETwork. Net::FTP=GLOB(0x22d19c)<<< 250- Net::FTP=GLOB(0x22d19c)<<< 250- Net::FTP=GLOB(0x22d19c)<<< 250 Directory successfully changed. Net::FTP=GLOB(0x22d19c)>>> PORT 164,48,254,74,158,171 Net::FTP=GLOB(0x22d19c)<<< 200 PORT command successful. Consider using + PASV. Net::FTP=GLOB(0x22d19c)>>> RETR README.html Net::FTP=GLOB(0x22d19c)<<< 150 Opening BINARY mode data connection for + README.html (5804 bytes). Net::FTP=GLOB(0x22d19c)<<< 226 File send OK.
        Maybe you can try active mode and see if that works. You can do this using the following line:
        my $ftp=Net::FTP->new("ftp.cpan.org", Debug => 1, Passive => 0)
        Hope that helps.
Re: Problem with Net::FTP
by regexes (Hermit) on Jul 10, 2007 at 11:53 UTC
    Hello,

    You didn't actually mention on which operating system this is being run, so to see the environmental variables of the user running the script:

    Unix/Linux
    at the command line type: env or env | grep FTP_FIREWALL


    Windows
    on the command line type: set


    The Net::FTP documentation state this:
    "OPTIONS" are passed in a hash like fashion, using key and value pairs.


    So...

    use warnings; use strict; use Net::FTP; my $ftp=Net::FTP->new(Host => "ftp.cpan.org", Firewall => "XXXX") or die "Couldnt connect:$@\n"; $ftp->login("anonymous"); $ftp->cwd("/pub/CPAN"); $ftp->get("README.html"); $ftp->close;


    That's how I understand the documention at least... :-)

    regexes
      Thank you for your time regexes,
      Yes I am using Linux OS, Fedora Core-3.
Re: Problem with Net::FTP
by syphilis (Bishop) on Jul 10, 2007 at 13:55 UTC
    After execution of script, it gives me doller prompt again!!

    That's exactly all that should happen - though you should also then find a copy of 'README.html' in your current working directory.

    Cheers,
    Rob

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2020-02-25 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (108 votes). Check out past polls.

    Notices?