Posts Tagged ‘FTP files’

FTP files with Perl and NET::FTP

net ftp perl

Needed to run some reports out of Oracle then FTP them. Could probably do this easier with normal shell command or something but I only have access to Perl at work. So thankfully NET::FTP is an installed module which made this quite painless.

The main thing to take away from this is the parameters you can use for NET::FTP, mainly using passive mode which I found a big help when the FTP server blocked active mode requests. Also to make this useful in any way, writing debug/ error tracking logic is very very useful since we are dealing with external servers and numerous issues can arise.

Whilst I have used an array to store my files, you can of course do a look up on your directory and pull all files from directory X or something similar instead. But in my instance the files are always the same and overwritten each time with new data so I don’t need to worry about archiving or changing filenames.

You can read more at:

The Code is below:

use Net::FTP;
use strict;
use warnings;

my $host="";
my @ERRORS = "";
my $newerr = "";
my @files = ("file1", "file2", "file3");

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

$ftp->login("username","password") or $newerr=1;
  push @ERRORS, "Can't login at $host: $!n" if $newerr;
$ftp->quit if $newerr;

myerr() if $newerr;
  print "Logged inn";

foreach my $file(@files) {
  $ftp->put($file, $file) or $newerr=1;
  push @ERRORS, "Can't the copy files  $!n" if $newerr;
  $ftp->quit if $newerr;
  myerr() if $newerr;

  print "Copied the filesn";

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

First we declare our module then we say we’re going to use strict and warnings which are very hadny for trapping errors and typos and feeding back to you.

Next we declare a few variables – one to store our FTP server, an array to store our errors, a variable to store if there’s a new error to add to the array and finally an array to store our file list.

We then initialise our NET::FTP object and attempt a connection to the server – you can specify many more settings in your connection that I have. Here we’re just checking to see if the server exists and we can ping it.

Next we attempt a login with a username and password and alert us if the login attempt is unsuccessful.

And then we loop through our file array and for each file we use the NET::FTP ‘put’ function, which copies the file to server, if it fails then alert us that there’s been a problem. To do this error tracking we are using push and basically this for adding a value to the end of an array by treating it as a list.

There are many other functions we could call here with our NET::FTP object like cwd(), pwd(), delete() and rename() functions to name but a few.

Anyway at the end of this we then loop through our array that stores errors and then print them out should an error occur. So you can see this is actually pretty easy to get started with.