Monday, August 13, 2012

How do you implement auto-login FTP process in Perl via a proxy (.netrc)?

#!/usr/bin/perl -w

use Net::FTP;
use Net::Netrc;
use Logger::Simple;

# Declare and Define values for variables
my $logfile="/home/logs/$0.log";
my $log=Logger::Simple->new(LOG=>$logfile);

# Get the login info from the .netrc file located in HOME directory
my $RemoteHost=Net::Netrc->lookup('<Server Name>');
my $User=$NT->login;
my $Pass=$NT->password;

# Craete the FTP object
my $ftp=new Net::FTP($RemoteHost, Debug => 1);

# Log into Server and list files
if(! $ftp->login($User,$Pass) )
{
  $log->write("Unable to connect to $RemoteHost!");
  die $ftp->message;
}
else
{
  $log->write("Successfully Connected to $RemoteHost");
  $ftp->ascii();
  $ftp->cwd("/HOME/nayakr/project/perl");
  @list=$ftp->ls('-lart');
  $ftp->close();
}
foreach (@list)
{
        print $_;
}

No comments :