Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
FTP from Perl?

Posted by enquirer (enquirer), 4 September 2002
"Can I automatically transfer a file by FTP from witihn a Perl program?"

Posted by admin (Graham Ellis), 4 September 2002
One of the most common search engine enquiries that brings people our way is "Perl FTP".  It's exceptionally easy to do, but the module you need (Net::FTP) is hidden within a group of other modules in the libnet bundle on the CPAN.  That's a lower case "l" for lib, unlike most other CPAN names, so you'll often find it listed like a lost soul on the very end of module lists!

Here's an introductory sample that transfers a file from a host computer called seal to the computer on which the Perl program is running, and reports back the size of the file transferred.

Code:
use Net::FTP;
print "retrieving file from seal\n";
$ftpobj = Net::FTP -> new ("seal");
$ftpobj -> login("p1","abc123");
$ftpobj -> cwd ("/export/home/www/cosite/about");
$ftpobj -> get ("melksham.html");
$ftpobj -> quit;
print "file size ",-s "melksham.html"," bytes\n";


Posted by bdulfer (bdulfer), 11 October 2002
Another possibility is LWP::UserAgent.
It is easier to use if you have a URL and not a server/path/filename combination.

Example:

#!/perl/bin/perl

use strict;
use warnings;

use LWP::UserAgent;


my $ua = LWP::UserAgent->new;
my $URL = 'ftp://ftp.abc.de/pub/anyfile';

my $req = HTTP::Request->new(GET => $URL);

my $contents;

$ua->request($req,
                                sub {
                                        ($contents, undef) = @_;

# the contents of the file is now in $contents
# you can save it to a file, send it via email, and whatnot

                              });




Posted by admin (Graham Ellis), 13 October 2002
Indeed - LWP::UserAgent is an excellent alternative, and has a better user interface in many (but not all) circumstances.

Typical Perl - there'e half a dozen ways of doing anything  

Posted by gbelday (gbelday), 19 August 2005
How would I ftp to mainframe?  I am using the follwing code but the put command isn't working..please help.
connection works but put doesn't. What should the put command be when i am ftping to mainframe?
ftping manually works...


       print "copying file to mainframe\n";
       $ftpobj = Net::FTP -> new ("xxx") or die "Cannot connect to xxx: $!";

       $ftpobj -> login("xxx","xxx")  or die "Cannot login to ftp ", $ftpobj->message;

       $filename = "xxx";

       print "copying file";
       $ftpobj -> put ("$filename", "'TST.PSNR003N.ISTTRADE.ALLOC.UPLOAD' (REPL") or die "copy failed ", $ftpobj->message;


Posted by admin (Graham Ellis), 19 August 2005
Have you got the Net::FTP Module loaded?  Have you "use"d it?  That's just a guess ... if you have got it loaded, please post up details of how your code fails - that's a piece of vital evidence that will help me advise you better

Posted by gbelday (gbelday), 20 August 2005
Thanks a lot for replying.  

1) Yes, the Net::FTP module is loading correctly.  I check each of the line below by giving invalid source, id, password and I get back messages correctly that it can't connect.  When I give the correct id and pw, everything is fine.

2) The code that is actually failing is the put command.  This code works fine when I ftp to a solaris box but fails when i ftp to mainframe.

This line below gives a message says invalid parentheses.  I don't have the exact error message as I am not at work. I can post the exact message tomorrow.  
 
  print "copying file";
  $ftpobj -> put ("$filename", "'TST.PSNR003N.ISTTRADE.ALLOC.UPLOAD' (REPL") or die "copy failed ", $ftpobj->message;

When I changed the code to look like below(i took out the single quotes), i get a message saying invalid dataset name for mainframe...  The same code works manually or though a korn shell script.  Notice the parentheses missing after REPL...this is the correct syntax.

  $ftpobj -> put ("$filename", "TST.PSNR003N.ISTTRADE.ALLOC.UPLOAD (REPL") or die "copy failed ", $ftpobj->message;

In korn shell, the follwing command works
put $filename 'TST.PSNR003N.ISTTRADE.ALLOC.UPLOAD' (REPL


Please help if you can!!  I'll post exact message when i get to work on monday.

Posted by admin (Graham Ellis), 20 August 2005
In your manual put, you give three parameters, the third of which is (REPL.  In the automated one, you've included the (REPL as the end of the second parameter. Subtle difference!

It's like me saying I have friends ...
Anne Marie Emma and Louise
.... looks like four friends.  But if I say ...
"Anne Marie" "Emma" and "Louise"
.... you can see it's just three.

Solution - remove the [space](REPL from the end of your automated put.  Now ... I'm not actually sure what the third parameter on your put means (it's FTP client dependent); it may simply be a request to replace an existing file without saying "are you sure" or it might be more significant.   If necessary, have a look at the docs for the ftp client to find out, then look for the equivalent extra call (I'm sure that's what it would be) in Net::FTP.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho