Monday, September 26, 2011

Write script in Perl to fetch list of files from a specified server using FTP.

#!/usr/bin/perl

use Net::FTP;
use CGI qw(:standard);
$cgi = CGI->new;
$me=`basename $0`;
chomp($me);
$LOG="$me." . "log";
my $REMOTE_SERVER="<ServerName>";
my $login="<UserName>";
my $password="<Password>";
print header(-type=>'text/html'),           # create the HTTP header
      start_html(-title=>'List Files of a Directory of a Remote Server',  # start the HTML
                      -style=>{-code=>'body {background-color: #D6DAFE}
                h1 {font-size: 36pt}
                h2 {color: blue}
                h3 {color : green}
                p {margin-left: 50px}'});
   
print $cgi->start_form(-name=>'PFORM', -method=>'GET', -action=>$action);
$ftp = Net::FTP->new($REMOTE_SERVER, Debug => 1);
$ftp->login($login, $password) or die $ftp->message;
$CURRENT_DIR=$ftp->pwd();
chomp($CURRENT_DIR);
@list=$ftp->ls('-lart');
$ftp->quit() or warn "Couldn't quit.\n";
print $cgi->h3('File List for server :', $REMOTE_SERVER, 'for directory :', $CURRENT_DIR);
print $cgi->hr();
foreach (@list)
{
        print $cgi->h6($_);
}
print $cgi->end_form;
print end_html;

No comments :