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

Hie friends.... I need a little bit of help on writing a script to automatically ftp a file using cron job. is it possible not to use any library/module e.g. Net::FTP just pure perl scripts Cheers, Darren Gan

Florist In Malaysia

Replies are listed 'Best First'.
Re: ftp scripts
by muntfish (Chaplain) on Aug 26, 2005 at 08:14 UTC

    As others have suggested, it is worth spending a moment checking whether you already have Net::FTP. You don't mention what OS you're on but if it's Windows and you've got ActiveState Perl installed, then Net::FTP comes as standard, so you're covered.

    Having said that, it is possible to "script" the standard 'ftp' program on both Windows and Unix (thereby not worrying about sockets or RFCs). The Windows binary (ftp.exe) takes a -s command line option which specifies a file containing FTP commands. In Unix you can pipe commands in from stdin:

    ftp -inv <<EOF open myhost user myuser mypassword cd somedir get afile quit EOF

    That's actually a shell script solution but you could do something very similar in perl.

    Hope that helps - apologies if I've missed the point.


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&

      I forgot to point out that by using this method (passing a list of commands to the OS's standard FTP program) you make error-handling a whole lot more difficult. With Net::FTP you can check for errors programatically after each step, and abort as soon as something fails. With this method you end up running all the commands, even if the very first (or even the login) fails, which generates a huge pile of spurious errors. Then you need to capture the output from the ftp program (into a file, or pipe it in) then parse it for strings that look like errors - usually but not always a three-digit code at the start of a line... SFTP is even worse...

      I think I've successfully argued against my own solution - so make best efforts to find/install Net::FTP before resorting to this way!


      s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
Re: ftp scripts
by hubb0r (Pilgrim) on Aug 26, 2005 at 07:06 UTC
    It's certainly possible to roll your own code for an ftp client, but given the complexity of the task and the learning curve of getting it right, why? There are a plethora of modules available on CPAN for almost any situation in the ftp client arena.

    What do you have against using modules?

    If you really want to do it yourself, you're going to have to start reading the ftp rfcs, and work with raw sockets, I think.
      Hie hubb0r, does all this modules comes with perl by default? (sorry, i am totally new to perl, i am a ASP guy trying to learn perl). i am working on a huge production server which i can't just simply install a new module. thats why i need to have some raw scripts.. Thanks for sharing with me... Cheers, Darren

      Florist In Malaysia

        Have you checked to see if Net::FTP is available? Maybe you don't even have to worry about all this stuff. If you can read the documentation, you have the module.

        perldoc Net:::FTP

        Failing that, make friends with an admin. :)

        --
        brian d foy <brian@stonehenge.com>
        Subscribe to The Perl Review
        If you can install a new script, you should be able to install most modules too.

        Anyway, Net::FTP is not part of the perl core distribution, but its libnet package is so useful that it's practically always available on machines with a perl install anyway. At the very least, it should be available as a system package (rpm, deb, ppm, whatever) for any system you'd want to run a production server on.

        Keep in mind that modules don't have to be installed system-wide; you can install a private copy of Net::FTP in a private directory, and put use lib '/path/to/private/dir'; at the top of your script to use it.
Re: ftp scripts
by pg (Canon) on Aug 26, 2005 at 07:03 UTC

    This should give you the basic idea of Net::FTP.

    sub ftp_order_file { my $order_file = shift; my $ftp = new Net::FTP("hostname") || die "fail"; $ftp->login("userid", "password"); $ftp->cwd("/aa/a00/rel2.4/env/AATS1/GlomOrders"); $ftp->get($order_file); $ftp->close(); }

    Put is not much different, just read the document.

Re: ftp scripts
by johnnywang (Priest) on Aug 26, 2005 at 17:08 UTC
    Is there a reason you want to use perl to do this? At least on linux, I just use curl:
    /usr/bin/curl -T yourfile ftp://your.destination -u name:password