Perl NET::FTP module example

FTP or File Transfer Protocol is a way of transferring files between two computers. Using the protocol involves a client and a server software running. The client connects to the server box and can fetch or upload files on the server box. The default port for FTP is 21.

Perl has a Net::FTP module which acts as FTP client. This can be used to automate the transfer of files. and also for mirroring a copy of remote directory with the local directory.
Net::FTP is an object oriented module and has several methods for transfer of files, directory listing, etc. Initiating a connection to the ftp server is done with the following line:
Net::FTP->new("ftp.server.com")
This returns a ftp object we use to make the method calls for transferring files, etc.

Here I have noted down a basic program using the module with some of the important methods. The description about the methods are provided in-line as comments.

use Net::FTP;

## Check error message in $@
my $ftp = Net::FTP->new("no.server.addrs") || warn "\nFTP connect failed..: $@\n";

print "\n### Connecting with proper host address, and cheking other methods ###\n";
my $ftp = Net::FTP->new("192.168.56.102") || die "FTP connect failed: $@\n";
$ftp->login("ftpuser", "password") || die "Login failed...\n";

## Getting the path on remote server
my $dir = $ftp->pwd || warn "Can't get current directory:", $ftp->message;
print "\nThe work directory at remote ftp server is: $dir\n";

## Get a listing of the files and folder in the current directory
my $lof = $ftp->ls || warn "Unable to List the files/folders:", $ftp->message;
print "\nList of files/folders:\n@{$lof}\n";

## Creating new directory in remote server, the ftp user should have permission to do so. Returns the full-pathname of the new directory.
my $newdir = $ftp->mkdir("TestFtp") || warn "Unable to create directory:", $ftp->message;
print "\nCreated a dir: $newdir\n";
my $lof = $ftp->ls || warn "Unable to List the files/folders:", $ftp->message;
print "List of files/folders after create dir:\n@{$lof}\n";

my $remfile = ${$lof}[0];
## Get file size of remote file
my $size = $ftp->size($remfile) || warn "Unable to get size of remote file:", $ftp->message;
printf ("\nSize of remote file, $remfile is: %.2f MB \n", $size/1024);

my $remfile2 = ${$lof}[3];
## Get the remote file, returns the filename thats created locally if successful
my $get = $ftp->get($remfile2) || warn "Unable to get file:", $ftp->message;
print "\nGet the file locally: $get\n";

## Change directory in remote server
$ftp->cwd($newdir) || warn "Unable to change dir:", $ftp->message;
my $dir = $ftp->pwd || warn "Can't get new directory path:", $ftp->message;
print "\nChanged the work directory to: $dir\n";

## Put file in remote directory. Returns created remote filename if successful
my $put = $ftp->put("TestFtpMod.pl") || warn "Can't put file in remote:", $ftp->message;
print "\nStatus of put file: $put\n";
my $lof = $ftp->ls || warn "Unable to List the files/folders:", $ftp->message;
print "List of files/folders in $dir:\n@{$lof}\n";

## Rename a file in remote directory
$ftp->rename("TestFtpMod.pl","filerenamed.txt") || warn "Unable to rename file:", $ftp->message;
my $lof = $ftp->ls || warn "Unable to List the files/folders:", $ftp->message;
print "\nAfter Rename: List of files in $dir:\n@{$lof}\n";

## Deleting a file in remote directory
$ftp->delete("filerenamed.txt") || warn "Unable to delete file:", $ftp->message;
my $lof = $ftp->ls || warn "Unable to List the files/folders:", $ftp->message;
print "\nAfter delete: List of files:\n@{$lof}\n";

## Change directory to one level up or parent directory
$ftp->cwd("..") || warn "Unable to change to parent dir:", $ftp->message;
my $dir = $ftp->pwd || warn "Can't get new directory path:", $ftp->message;
print "\nAfter change of dir it is now at: $dir\n";

## Deleting a directory including its contents
$ftp->rmdir($newdir, true) || warn "Unable to remove directory:", $ftp->message;
print "\nDeleted directory with contents: $newdir\n";
my $lof = $ftp->ls || warn "Unable to List the files/folders:", $ftp->message;
print "List of files/folders now:\n@{$lof}\n";

## Its good to disconnect gracefully :)
$ftp->quit() || warn "Oops, Cannot quit!!\n";

A sample run of the program as below.
First check what is available in local folder:
$ ls
NetFtp_module.txt  TestFtpMod.pl

Execute the program:
$ perl TestFtpMod.pl
FTP connect failed..: Net::FTP: Bad hostname 'no.server.addrs'

### Connecting with proper host address, and cheking other methods ###
 The work directory at remote ftp server is: /home/ftpdir

List of files/folders:
10201_database_linux32.zip database filerename.txt perl_modules perl_script

Created a dir: TestFtp
List of files/folders after create dir:
10201_database_linux32.zip TestFtp database filerename.txt perl_modules perl_script

Size of remote file, 10201_database_linux32.zip is: 653060.55 MB

Get the file locally: filerename.txt

Changed the work directory to: /home/ftpdir/TestFtp

Status of put file: TestFtpMod.pl
List of files/folders in /home/ftpdir/TestFtp:
TestFtpMod.pl

After Rename: List of files in /home/ftpdir/TestFtp:
filerenamed.txt

After delete: List of files:


After change of dir it is now at: /home/ftpdir

Deleted directory with contents: TestFtp
List of files/folders now:
10201_database_linux32.zip database filerename.txt perl_modules perl_script

Now list the local folder after running the program:
$ ls
NetFtp_module.txt  TestFtpMod.pl  filerename.txt

No comments:

Post a Comment