ftp perl script using Net::FTP

—————-Theory:———————–

File Transfer Protocol

File Transport Protocol, or FTP, is an open protocol standard that is widely used to transport and receive large files. FTP can be used to send and receive large files. FTP can also be used to send configuration files and software updates for network switches and routers. It uses ports for communications and also uses encryption to protect the information being received and sent. In this lesson, we will go over these topics so that you can be familiar with FTP and what it has to offer.

How it Works!

The picture above shows the generic operation of how a client accesses an FTP server. The FTP server listens to Port 21 for connection requests and it sends data from its Port 20.
ftp1

Establishing the FTP Connection using Port 21 on the FTP Server

1. The FTP Client opens a random port on itself to initiate the request. In this case, the FTP client chooses Port 1.

2. The FTP Client will then forward its requests for FTP connection using its own Port 1 (this is used to tell the FTP server what to do i.e., ‘Download’, ‘Stop’ and ‘Cancel Download’) to the FTP server which is listening on Port 21.

3. Once the FTP server receives the FTP connection requests on its Port 21, the FTP Server will then send an acknowledgment back to the FTP Client using its address of Port 1 saying that the connection is now established and download may commence.

At this point, the connection is only established between the FTP Client and the FTP Server.

The FTP server uses Port 21 only for connection setup and receiving commands from the FTP client. The next step is downloading data from the FTP server to the FTP client.

——————-:Script:———————-

Basic FTP script :-

simple perl script using Net::FTP module

install the Net::FTP module using below command

#curl -L http://cpanmin.us | perl - --sudo Net::FTP

#vim ftp.pl

(copy the below code and paste)

#!/usr/bin/perl
use strict;
use warnings;
#---------install module before running the script------------
use Net::FTP;
my $hostname = 'localhost';
my $username = 'user1';
my $password = 'wifi123#';
#------pass hostname or ip address----------------
my $ftp = Net::FTP->new($hostname);
#------credentials for login-------------------------
 $ftp->login($username, $password);
#-------print presnt working directory---------------
my $pwd = $ftp->pwd;
print "$pwd \n---------------------------------------\n";
#-------print present working dir file or dirs---------
my @list = $ftp->ls;
print "@list\n---------------------------------------\n";
#-------change the dir and list file or dirs----------
my $chdir = $ftp->cwd('serverfile');
my @list1 = $ftp->ls('-lR');
print "@list1\n---------------------------------------\n";

O/p:-

get code from GIT- click here

 

 

 

 

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s