Automate FTP upload & download using perl and stor

2019-09-19 13:20发布

问题:

I am trying to automate the FTP upload/download from local system to FTP server and calculate the bandwidth or some statistics.

Requirements:

  1. Upload some file (say 10 MB file) to FTP server.
  2. Add some sleep/wait time, download the same file from the server. Repeat point 1 & 2 for n number of loops
  3. Calculate the FTP upload throughput & download throughput each iteration and save it in a notepad or excel.

Code:

use strict;
use warnings;
use Net::FTP;
use Time::Piece;
use Time::Seconds qw/ ONE_DAY /;

my ($ftp, $host, $user, $pass, $dir, $fpath);

$host = "hostname";
$user = "username";
$pass = "password";
$dir  = "/home/file/user";
$fpath = "E:/Automation/tesfiles/10meg.test";

$ftp = Net::FTP->new($host, Debug => 0);
$ftp->login($user, $pass) or die $ftp->message;
$ftp->cwd($dir);

#FTP Upload
my $upload_start = localtime;
$ftp->put($fpath) or die $ftp->message;
print $ftp->message;
my $upload_end = localtime;
my $upload_time = ($upload_end - $upload_start)->pretty;

my $fname = '10meg.test';
my $size = $ftp->size( $fname );
defined( $size ) or die( "  Failed to get size: " . $ftp->message );

#FTP Download
my $download_start = localtime;
print "Size of $fname is $size\n";
$ftp->get($fname) or die $ftp->message;
print $ftp->message;
my $download_end = localtime;
my $download_time = ($download_end - $download_start)->pretty;


print "Time taken for FTP download is $upload_time for $fname file\n";
print "Time taken for FTP upload is $download_time for $fname file\n";
print "Hence Throughput for Download is: $size/$download_time\n";
print "Hence Throughput for Upload is: $size/$upload_time\n";

Console Result:

Transfer complete.
Size of 10meg.test is 10038600
Transfer complete.
Time taken for FTP download is 43 seconds for 10meg.test file
Time taken for FTP upload is 5 seconds for 10meg.test file
Hence Throughput for Download is: 10038600/43 seconds
Hence Throughput for Upload is: 10038600/5 seconds

Issues:

a. See the throughput are not numerical numbers.
b. I need to re-direct the output to excel or notepad.
c. I need to repeat the process in loop.

P.S:

I am completely new in Perl but somehow I managed a script(source: SO) that uploads a file to FTP server.

回答1:

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

my ($ftp, $host, $user, $pass, $dir, $fpath);

$host = "hostname";
$user = "username";
$pass = "password";
$dir  = "/home/file/user";
$fpath = "E:/Automation/testfiles/10meg.test";
$ftp = Net::FTP->new($host, Debug => 0);
$ftp->login($user, $pass) or die $ftp->message;
$ftp->cwd($dir);
$ftp->put($fpath) or die $ftp->message;

print $ftp->message;

my $fname = '10meg.test';

my $size = $ftp->size( $fname );
defined( $size ) or die( "  Failed to get size: " . $ftp->message );

my $transfer_start = time;
print "  Size of $fname is $size\n";
$ftp->get($fname) or die $ftp->message;

print $ftp->message;

my $transfer_end = time;

my $time_taken = $transfer_end - $transfer_start;

print "Time take to download of $fname is $time_taken\n";

print "Transfer speed $size/$time_taken\n";

There is an option to the ->New() function called Hash that will notify you every time 1024 bytes are transferred. Read about it!