Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Perl Net::FTP module

News Recommended Links Reference Sort in Perl
Regular expressions History Humor Etc

Using Net::FTP, which is a standard Perl module, you can write simple FTP clients that transfer files. Here is an example of a client that connects to a remote FTP server and gets a file from the server:

Simple example:

#!/usr/local/bin/perl -w

use Net::FTP;

$hostname = 'yourhost';
$username = 'youname';
$password = 'yourpass';

# Hardcode the directory and filename to get
$home = `pwd`;
$target='/home/usr/public_html';
$filename = 'index.shtml';

# Open the connection to the host
$ftp = Net::FTP->new($hostname) or die("Can't connect to the server\n"); # construct object
$ftp->login($username, $password) or die("Can't login to the account $username"); # log in
print "Connected to $username\@$hostname \n";

# Now let's put file to the ftp servere and leave

   put_file($file);  
   $ftp->quit;
   exit;
sub put_file {
   $filename=substr($file, $last);
   $path=substr($file,0,$last);
   $ftp->cwd($path);
   $ftp->put($filename); 
   @ls=$ftp->ls("$path/$filename";
   if (index (@ls[0]),$filename) > -1) {
       print "File $filename was transferred successfully to the $path directory\n";
       return 1;
   } else {
       print File transfer of $filename faled\n";
       return 0;
   }

More complex variant

#!/usr/local/bin/perl -w

use Net::FTP;

$hostname = 'yourhost';
$username = 'youname';
$password = 'yourpass';

# Hardcode the directory and filename to get

$target='/home/usr/public_html';
$filename = 'index.shtml';

# Open the connection to the host
   connect($hostname,$username,$password); 
# Now let's put files to the ftp servere and leave
while($file=<SYSIN>) {
   chomp($file);
   $last=rindex($file,"/");
   unless($last>0) {
     print "wrong file $file\n";
     next;
   }
   put_file($file);  
} # for
$ftp->quit;
exit;
sub connect 
{
$ertype='';
$ftp = Net::FTP->new($hostname) or die("Can't' connect to the server\n"); # construct object
$ftp->login($username, $password) or die("Can't login to the account $username"); # log in
print "Connected to $username\@$hostname \n";
}
sub put_file 
{
my ertype='';
 $filename=substr($file, $last);
   $path=substr($file,0,$last);
   $ftp->cwd($path) || ertype="path $path" doe not exit of target computer\n";
   return 0 if ($ertype);
   $ftp->put($filename);    
   if (index ($ftp->ls("$path/$filename"),$filename) > -1) {
       print "File $filename was transferred successfully to the $path directory\n";
       return 1;
   } else {
       print File transfer of $filename faled\n";
       return 0;
   }
} # sub
FTP clients have also been integrated with most World Wide Web browsers, using ftp:// in place of http://. When the URL points to a directory, the browser displays a listing of the directory, where each filename is a link to that file. When the URL points directly to a file, the remote file is downloaded.

Here's an example that uses Net::FTP to list files from a remote FTP server on a web page, with a link from each file to the URL of the file on the remote site:

#!/usr/local/bin/perl -w

use Net::FTP;

$hostname = 'remotehost.com';           # ftp host
$username = 'anonymous';                # username
$password = 'myname@mydomain.com';      # password
$home = '/pub';

$ftp = Net::FTP->new($hostname);        # Net::FTP constructor
$ftp->login($username, $password);      # log in w/username and password

$pwd = $ftp->pwd;                       # get current directory

# Now, output HTML page.
print <<HTML;
Content-type: text/html
<HTML>
  
  <HEAD>
    <TITLE>Download Files</TITLE>
  </HEAD>
  <BODY>
      
      <B>Current working directory:</B> $pwd<BR>
      Files to download: <P>
HTML
  
    @entries = $ftp->ls($home);       # slurp all entries into an array
    foreach (@entries) { # now, output links for all files in the ftp area
                         # as links
        print "<INPUT TYPE=hidden NAME=\"files\" VALUE=\"$_\">\n";
        print "<A HREF=\"ftp://$hostname$_\">",
        "<IMG SRC=\"http://www/icons/f.gif\" border=0>\n";
        print " $_</A><BR>\n";
    }
    print <<HTML;
  </BODY>
</HTML>
HTML
$ftp->quit;             # end FTP session
The Net::FTP module implements a subset (as shown earlier in this chapter) of the FTP protocol as defined in RFC 959. In addition to providing the methods shown below, the module inherits from Net::Cmd. Some of the Net::FTP methods return an object derived from the dataconn class (which is in turn derived from the IO::Socket::INET class), as noted in the entries for those methods.

The following methods are defined by Net::FTP:

FTP Configuration with Net::Netrc

Unix-based FTP clients use a file called .netrc, which you can configure to automate FTP access to sites you frequently visit. With a properly defined .netrc file, you can simply execute the FTP command to a favorite FTP host and be automatically logged in to the FTP server. Your .netrc file contains one line for each connection you want to be able to make. The following tokens can be specified for each entry:

 

machine name

Specifies a remote machine to which you want to autologin:

machine remotehost.com
Instead of machine name, you can specify the single word default to match any machine name. This is usually used for anonymous logins to machines not listed in .netrc.

 

login name

If present, identifies the user who logs in to the remote machine, where name is the login name.

password passwd

If present, gives the password for the user. The autologin process uses the specified password if the remote server requires one.

account acctpw

If present, gives an additional password for the user. The autologin process uses this password if the remote server requires one.

macdef name

If present, defines a macro with the specified name. Net::Netrc simply parses this field, in order to maintain compatibility with FTP.

Here's an example of a typical .netrc entry:

machine remotehost.com login username password userpasswd
Entering your username and password for remote sites in unencrypted form has serious security implications. Many sites consider .netrc files a violation of security policy and do not allow them. In addition, most FTP clients require that the .netrc file be owned by you and readable only by you, with permissions set to 0400 or 0600. If the permissions aren't correctly set, the autologin process aborts. Net::Netrc follows this security convention - if the permissions and ownership are not correct, the .netrc file isn't read, and a warning message is issued.

 

Net::Netrc implements a simple interface to the .netrc file, telling you which hosts you can automatically connect to. It doesn't actually connect to any remote hosts; it's simply used to query your configuration file and return the value of the tokens.

The following methods are supplied by Net::Netrc:

Old News

Beginners guide to NetFTP

Presentation itself is junk, only comments have value

Re: Beginners guide to Net::FTP
by runrig (Monsignor) on Aug 14, 2002 at 17:16 UTC
    Its good that you check the status of the FTP calls, but if they do fail, you may want to include the specific error message (which many mistakenly believe is in $! but is not). On the Net::FTP->new() call, the error message is in $@, and after you have a valid Net::FTP object (e.g. $ftp), you can call $ftp->message.

    Another common mistake is to wrap the $ftp->message in quotes, e.g. $ftp->login('user','passwd') or die "Couldn't login: $ftp->message". If you do that, you'll get a very unhelpful error message :-)

      Which can always be remdied by some further wrapping. :-) $ftp->login('user','passwd') or die "Couldn't login: @{[ $ftp->message ]}";
Re: Beginners guide to Net::FTP
by jbowshaw (Initiate) on Mar 21, 2008 at 15:24 UTC
    Even though this is a beginner topic. It would be nice to see how to use the use Net::FTP::AutoReconnect;
    function call.
     
    The reason being is because in my project I have a 3.5 gig file to download daily. If FTP resets, I would like to know the looping structure for AutoReconnect and to restart where the FTP transfer stopped because of a connection issue.

 

FTP from Perl - Perl Programming

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.
 

 

Perl NetFTP

Here's the first:

#!/usr/bin/perl
use Net::FTP;

my $host="aplawrence.com";
my $directory="pub";


$ftp=Net::FTP->new($host,Timeout=>240) or $newerr=1;
push @ERRORS, "Can't ftp to $host: $!\n" if $newerr;
myerr() if $newerr;
print "Connected\n";


$ftp->login("ftp","apl\@") or $newerr=1;
print "Getting file list";
  push @ERRORS, "Can't login to $host: $!\n" if $newerr;
  $ftp->quit if $newerr;
  myerr() if $newerr; 
print "Logged in\n";


$ftp->cwd($directory) or $newerr=1; 
  push @ERRORS, "Can't cd  $!\n" if $newerr;
  myerr() if $newerr;
  $ftp->quit if $newerr;


@files=$ftp->dir or $newerr=1;
  push @ERRORS, "Can't get file list $!\n" if $newerr;
  myerr() if $newerr;
print "Got  file list\n";   
foreach(@files) {
  print "$_\n";
  }
$ftp->quit;


sub myerr {
  print "Error: \n";
  print @ERRORS;
  exit 0;
}
 

Pretty simple, right? Net::FTP makes it all so easy, so let's do something that would absolutely drive me batty without it.

#!/usr/bin/perl
use Net::FTP;
$date=shift @ARGV;
@months=qw(null Jan Feb Mar Apr My Jun Jul Aug Sep Oct Nov Dec);
@hosts=qw(pcunix.org pcunix.com xyz.com);
@dirs=qw(pub pub pub);
@logins=qw(ftp anonymous fred);
@passwords=qw(tony\@ apl\@ fxdfed);


$x=0;
foreach(@months) {
 $months{$_}=$x++;
 }
# we need this hash later

if (not $date) {
  $now=time();
  $now -= (24 * 3600 );
  # yesterday
  ($nowsec,$nowmin,$nowhr,$nowday,$nowmon,$nowyr,$nowdow,$nowdoy,$nowdst)=localtime($now);
  $nowyr+=1900;$nowmon++;
  $date=sprintf("%.2d/%.2d/%.4d",$nowmon,$nowday,$nowyr);
  print "Using $date\n";
  }



$now=time();
($nowsec,$nowmin,$nowhr,$nowday,$nowmon,$nowyr,$nowdow,$nowdoy,$nowdst)=localtime($now);
$nowyr+=1900;

 
# need $nowyr later 

($month,$day,$year)=split /\//,$date;

#
# broken next century - blame me then
#
$year+=2000 if $year < 100;

$x=0;
foreach (@hosts) {
   $newerr=0;
   $ftp=Net::FTP->new($_,Timeout=>240) or $newerr=1;
     push @ERRORS, "Can't ftp to $_: $!\n" if $newerr;
     next if $newerr;
   print "Connected $_\n";


   $ftp->login($logins[$x],$passwords[$x]) or $newerr=1;
     push @ERRORS, "Can't login to $_: $!\n" if $newerr;
     $ftp->quit if $newerr;
     next if $newerr;
   print "Logged in $_\n";

   $ftp->cwd($dirs[$x]) or $newerr=1;
     push @ERRORS, "Can't cd $dirs[$x] on $_ $!\n" if $newerr;
     $ftp->quit if $newerr;
     next if $newerr;
   print "Getting file list $_\n";


   @files=$ftp->dir or $newerr=1;
     push @ERRORS, "Can't get file list on $_ $!\n" if $newerr;
     $ftp->quit if $newerr;
     next if $newerr;
   print "Got list $_\n";

   print "Looking for $date $time\n";
   foreach(@files) {
    $_=substr($_,41);
    s/  */ /g;
    s/^  *//g;
    chomp;
    @stuff=split / /;
    # if it's today, the year slot will have time instead
    # so make it this year
    $stuff[2]=$nowyr if /:/;

    $ftp->quit if ($stuff[2] < $year);
    next if ($stuff[2] < $year);
    $ftp->quit if ($months{$stuff[0]} < $month and $stuff[2] == $year);
    next if ($months{$stuff[0]} < $month and $stuff[2] == $year);
    $ftp->quit if ($stuff[0] < $day and $stuff[2] == $year and $months{$stuff[0]} == $month);
    next if ($stuff[1] < $day and $stuff[2] == $year and $months{$stuff[0]} == $month);

    print "Getting $_\n";
    $ftp->get($stuff[3],$stuff[3]) or $newerr=1;
      push @ERRORS, "Couldn't get $stuff[3] $!\n" if $newerr;


   }

   $ftp->quit;
}

print @ERRORS;
exit 0;

Recommended Links

Perl FTP clients

Perl Net--FTP aplawrence.com/Unixart/perlnetftp.html

Beginners guide to NetFTP presendtaion is very weak, but some comments are insightful



Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Last modified: October 10, 2009