Topic: Upload a file via FTP , using Perl Object Oriented Programming . Check the uploaded file size upon successful in remote host.
Pre-request: Check Net::FTP package in your system, before use this ftp file upload tool.
Execute this linux command:
$perldoc -l Net::FTP
/usr/lib/perl5/5.8.8/Net/FTP.pm
$
It means already installed . Other do install it from RPM, or CPAN library.
After that you can copy the files below and enjoy make use of reliable FTP File Upload Tool.
Files :
1. ftp. pm - <perl ftp user defined class to upload file >
#*******************************************************
#Class Name: ftp
#Member Description:
# host -> Remote server IP Addr / Host Name
# username -> user name of remote server
# password -> password of remote server
# destpath -> destination path on remote server
# binary -> either transfer in binary mode
# timout -> ftp time out value
# debug -> ftp with debug log
# logfile -> log file name to write ftp logs
# filefd -> file descriptor of log file
#********************************************************
2. upload.pl - <upload a file to remote host using ftp class>
Copy both these files in a single directory. Change the ftp class params in upload.pl file with respect to need.
Fileame : ftp.pm
-----------------
#---------------------------------------------------
#Class Name: ftp
#Scope : file upload using ftp protocol
##---------------------------------------------------
use strict;
use warnings;
use Net::FTP;
package ftp;
#*******************************************************
#Class Name: ftp
#Member Description:
# host -> Remote server IP Addr / Host Name
# username -> user name of remote server
# password -> password of remote server
# destpath -> destination path on remote server
# binary -> either transfer in binary mode
# timout -> ftp time out value
# debug -> ftp with debug log
# logfile -> log file name to write ftp logs
# filefd -> file descriptor of log file
#********************************************************
#-----------------------------------------------------------
#@brief: constructor
#
#@param in: name of the class, required memebers of class
#
#@return value: object of the class
#
##-----------------------------------------------------------
sub new
{
my $class = shift;
my $self = {@_};
bless($self, $class);
$self->{filefd} = undef;
if( defined($self->{logfile}))
{
open($self->{filefd}, ">>$self->{logfile}" ) || die "couldn't open file $!";
}
return $self;
}
#-----------------------------------------------------------
#@brief: Class Method for connect and upload the file
# to specified destination path at remote server
#
#@param in: object of class, source filaname to be upload
#
#@return value:
# size of the file uploaded - on success
# -ve - for failure
#-----------------------------------------------------------
sub upload
{
my ($myclass, $filename) = @_;
my @date = ();
my $errno = undef;
my $ret = undef;
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
printf({$myclass->{filefd}} "%s :: connecting to host [%s]\n",
@date, $myclass->{host}) if defined($myclass->{filefd});
my $ftp = Net::FTP->new($myclass->{host}, Debug => $myclass->{debug}, Timeout => $myclass->{timeout});
if ( !defined ($ftp) )
{
$errno = -101;
printf({$myclass->{filefd}} "%s :: connect to host[%s] failed error[%d][%s]\n",
@date, $myclass->{host}, $errno, $!) if defined($myclass->{filefd});
return $errno;
}
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
printf({$myclass->{filefd}} "%s :: successfully connected to host [%s]\n",
@date, $myclass->{host}) if defined($myclass->{filefd});
if ( $ftp->login($myclass->{username},$myclass->{password}) == 0 )
{
$errno = -102;
printf({$myclass->{filefd}} "%s :: login failed username[%s] password[%s] error[%d][%s]\n",
@date, $myclass->{username}, $myclass->{password}, $errno, $!) if defined($myclass->{filefd});
return $errno;
}
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
printf({$myclass->{filefd}} "%s :: login success for username[%s]\n",
@date, $myclass->{username},) if defined($myclass->{filefd});
if ( $ftp->cwd($myclass->{destpath}) == 0 )
{
$errno = -103;
printf({$myclass->{filefd}} "%s :: couldn't change to destination directory[%s] error[%d][%s]\n",
@date, $myclass->{destpath}, $errno, $!) if defined($myclass->{filefd});
return $errno;
}
if ( $myclass->{binary} == 1 )
{
#$ftp->binary || die $ftp->message;
$ret = $ftp->binary;
if( !defined($ret) )
{
$errno = -104;
printf({$myclass->{filefd}} "%s :: setting binary mode failed to upload file[%s] error[%d][%s]\n",
@date, $filename, $errno, $!) if defined($myclass->{filefd});
return $errno;
}
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
printf({$myclass->{filefd}} "%s :: uploading file[%s] in binary mode\n",
@date, $filename) if defined($myclass->{filefd});
}
else
{
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
printf({$myclass->{filefd}} "%s :: uploading file[%s] in ascii mode\n",
@date, $filename) if defined($myclass->{filefd});
}
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
$ret = $ftp->put("$filename");
if ( !defined ($ret))
{
$errno = -105;
printf({$myclass->{filefd}} "%s :: failed to upload file[%s] error[%d][%s]\n",
@date, $filename, $errno, $!) if defined($myclass->{filefd});
return $errno;
}
$ret = $ftp->size($filename);
printf({$myclass->{filefd}} "%s :: file[%s] upload success size[%d]\n",
@date, $filename, $ret) if defined($myclass->{filefd});
$ftp->quit;
printf({$myclass->{filefd}} "-" x 100,"\n\n") if defined($myclass->{filefd});
printf({$myclass->{filefd}} "\n") if defined($myclass->{filefd});
close($myclass->{filefd}) if defined($myclass->{filefd});
return $ret;
}
1;
File Name: upload.pl
-------------------------
#!/usr/bin/perl
#------------------------------------------------------
#Scope: use ftp class & upload the file
# to specified destination path on remote server
##------------------------------------------------------
use warnings;
use strict;
use ftp;
my $sourcefile = "hello.txt";
#initialize the object
my $myobj = ftp->new(host => "12.1.1.1", username => "root", password => "root123",
binary => 1, timout => 120,
debug => 0, logfile => "/tmp/ftp.log", destpath => "/tmp");
my $filesize = -s $sourcefile;
my $uploadlen = $myobj->upload($sourcefile);
if($filesize == $uploadlen )
{
print "ftp upload file[$sourcefile] size[$uploadlen] success \n";
}
else
{
print "ftp upload file[$sourcefile] size[$uploadlen] failed \n";
}
Pre-request: Check Net::FTP package in your system, before use this ftp file upload tool.
Execute this linux command:
$perldoc -l Net::FTP
/usr/lib/perl5/5.8.8/Net/FTP.pm
$
It means already installed . Other do install it from RPM, or CPAN library.
After that you can copy the files below and enjoy make use of reliable FTP File Upload Tool.
Files :
1. ftp. pm - <perl ftp user defined class to upload file >
#*******************************************************
#Class Name: ftp
#Member Description:
# host -> Remote server IP Addr / Host Name
# username -> user name of remote server
# password -> password of remote server
# destpath -> destination path on remote server
# binary -> either transfer in binary mode
# timout -> ftp time out value
# debug -> ftp with debug log
# logfile -> log file name to write ftp logs
# filefd -> file descriptor of log file
#********************************************************
2. upload.pl - <upload a file to remote host using ftp class>
Copy both these files in a single directory. Change the ftp class params in upload.pl file with respect to need.
Fileame : ftp.pm
-----------------
#---------------------------------------------------
#Class Name: ftp
#Scope : file upload using ftp protocol
##---------------------------------------------------
use strict;
use warnings;
use Net::FTP;
package ftp;
#*******************************************************
#Class Name: ftp
#Member Description:
# host -> Remote server IP Addr / Host Name
# username -> user name of remote server
# password -> password of remote server
# destpath -> destination path on remote server
# binary -> either transfer in binary mode
# timout -> ftp time out value
# debug -> ftp with debug log
# logfile -> log file name to write ftp logs
# filefd -> file descriptor of log file
#********************************************************
#-----------------------------------------------------------
#@brief: constructor
#
#@param in: name of the class, required memebers of class
#
#@return value: object of the class
#
##-----------------------------------------------------------
sub new
{
my $class = shift;
my $self = {@_};
bless($self, $class);
$self->{filefd} = undef;
if( defined($self->{logfile}))
{
open($self->{filefd}, ">>$self->{logfile}" ) || die "couldn't open file $!";
}
return $self;
}
#-----------------------------------------------------------
#@brief: Class Method for connect and upload the file
# to specified destination path at remote server
#
#@param in: object of class, source filaname to be upload
#
#@return value:
# size of the file uploaded - on success
# -ve - for failure
#-----------------------------------------------------------
sub upload
{
my ($myclass, $filename) = @_;
my @date = ();
my $errno = undef;
my $ret = undef;
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
printf({$myclass->{filefd}} "%s :: connecting to host [%s]\n",
@date, $myclass->{host}) if defined($myclass->{filefd});
my $ftp = Net::FTP->new($myclass->{host}, Debug => $myclass->{debug}, Timeout => $myclass->{timeout});
if ( !defined ($ftp) )
{
$errno = -101;
printf({$myclass->{filefd}} "%s :: connect to host[%s] failed error[%d][%s]\n",
@date, $myclass->{host}, $errno, $!) if defined($myclass->{filefd});
return $errno;
}
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
printf({$myclass->{filefd}} "%s :: successfully connected to host [%s]\n",
@date, $myclass->{host}) if defined($myclass->{filefd});
if ( $ftp->login($myclass->{username},$myclass->{password}) == 0 )
{
$errno = -102;
printf({$myclass->{filefd}} "%s :: login failed username[%s] password[%s] error[%d][%s]\n",
@date, $myclass->{username}, $myclass->{password}, $errno, $!) if defined($myclass->{filefd});
return $errno;
}
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
printf({$myclass->{filefd}} "%s :: login success for username[%s]\n",
@date, $myclass->{username},) if defined($myclass->{filefd});
if ( $ftp->cwd($myclass->{destpath}) == 0 )
{
$errno = -103;
printf({$myclass->{filefd}} "%s :: couldn't change to destination directory[%s] error[%d][%s]\n",
@date, $myclass->{destpath}, $errno, $!) if defined($myclass->{filefd});
return $errno;
}
if ( $myclass->{binary} == 1 )
{
#$ftp->binary || die $ftp->message;
$ret = $ftp->binary;
if( !defined($ret) )
{
$errno = -104;
printf({$myclass->{filefd}} "%s :: setting binary mode failed to upload file[%s] error[%d][%s]\n",
@date, $filename, $errno, $!) if defined($myclass->{filefd});
return $errno;
}
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
printf({$myclass->{filefd}} "%s :: uploading file[%s] in binary mode\n",
@date, $filename) if defined($myclass->{filefd});
}
else
{
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
printf({$myclass->{filefd}} "%s :: uploading file[%s] in ascii mode\n",
@date, $filename) if defined($myclass->{filefd});
}
@date = `date "+%F %H:%M:%S"`;
chomp(@date);
$ret = $ftp->put("$filename");
if ( !defined ($ret))
{
$errno = -105;
printf({$myclass->{filefd}} "%s :: failed to upload file[%s] error[%d][%s]\n",
@date, $filename, $errno, $!) if defined($myclass->{filefd});
return $errno;
}
$ret = $ftp->size($filename);
printf({$myclass->{filefd}} "%s :: file[%s] upload success size[%d]\n",
@date, $filename, $ret) if defined($myclass->{filefd});
$ftp->quit;
printf({$myclass->{filefd}} "-" x 100,"\n\n") if defined($myclass->{filefd});
printf({$myclass->{filefd}} "\n") if defined($myclass->{filefd});
close($myclass->{filefd}) if defined($myclass->{filefd});
return $ret;
}
1;
File Name: upload.pl
-------------------------
#!/usr/bin/perl
#------------------------------------------------------
#Scope: use ftp class & upload the file
# to specified destination path on remote server
##------------------------------------------------------
use warnings;
use strict;
use ftp;
my $sourcefile = "hello.txt";
#initialize the object
my $myobj = ftp->new(host => "12.1.1.1", username => "root", password => "root123",
binary => 1, timout => 120,
debug => 0, logfile => "/tmp/ftp.log", destpath => "/tmp");
my $filesize = -s $sourcefile;
my $uploadlen = $myobj->upload($sourcefile);
if($filesize == $uploadlen )
{
print "ftp upload file[$sourcefile] size[$uploadlen] success \n";
}
else
{
print "ftp upload file[$sourcefile] size[$uploadlen] failed \n";
}