Operating System - HP-UX
1745809 Members
3973 Online
108722 Solutions
New Discussion

Help with perl NET::ftp script

 
Juan M Leon
Trusted Contributor

Help with perl NET::ftp script

Good morning, I need help with enabling error tracking or messaging execution success within the script.

The perl script (I attached the section that I need make some changes) runs a for loop and opens a ftp connection to group of servers to either send or get files on each ftp server. if a connection to a server fails it continues to the next server.

 

 

The script does a $FtpObject = Net::FTP->new($ServerHost{$Host}); then  $Ret=$FtpObject->login($User,$How); The problem I have is that sometimes I get  a message indicating that the login methond cannot be called with an undefined value and I dont know where is this originating but I suspect it is failing because of the FtpObject is not complete.

 

Error : Can't call method "login" on an undefined value at line 507

 

I would like to add:  or die "[Error] UNABLE TO CREATE FTP OBJECT: [$@]";

with in the FtpOpen line to get information as to which ftp server is failng to open and send e-mails to alert us.

 

can someone suggest the bets way to modify this part of the script.

Please let me know if my explanation needs more details or to be better explained.

 

thank you

 

 

 

 

 

###########################################################################
# Process for each host in our Hash array
###########################################################################

foreach $Host (keys(%ServerHost))
{
   if ( "$ServerHost{$Host}" eq "localhost" )
   {
      $UseFtp=0;
   }
   else # Must be remote ( Using FTP )
   {
      $UseFtp=1;
      #Force passive FTP 
      #if ( "$ServerHost{$Host}" eq "server.net" )
      if (  "$ServerHost{$Host}" eq "ftp.server.com" )
      {
         $FtpObject = Net::FTP->new($ServerHost{$Host}, Passive => 1, Debug => 1);
      }
      else
      {
         $FtpObject = Net::FTP->new($ServerHost{$Host});
      }
      ($User,$How)=split('/',$ServerUser{$Host});
      $Ret=$FtpObject->login($User,$How);   ## <<-----   Line 507
      if (! $Ret)
      {
         $LogFails{$Host}++;
         if ( $LogFails{$Host} <= 3 )
         {
            MailIt("$WhoFindsOut",
                   "Cash file import failed to login to Server: $Host",
                   "There was a problem attempting to login to the server: ".
                   "$Host");
         } elsif ( $LogFails{$Host} =~ /0\Z/ )   # Counts that end w/ zero
         {
            MailIt("$WhoFindsOut",
                   "Cash file import failed to login to Server: $Host",
                   "There is a reoccurring problem attempting to login to ".
                   "the server: $Host. Login has failed $LogFails{$Host} ".
                   "times.");
         }
         next;
      }else
      {
         if ( $LogFails{$Host} )
         {
            MailIt("$WhoFindsOut",
                   "Cash file import message: Server, $Host, back online.",
                   "Sucessfully logged into server: $Host, after ".
                   $LogFails{$Host}.
                   " failed login attempt(s).");
         }
         $LogFails{$Host}=0;
      }
   }

1 REPLY 1
ChatterjeeS
Visitor

Re: Help with perl NET::ftp script


@Juan M Leon wrote:

         $FtpObject = Net::FTP->new($ServerHost{$Host}, Passive => 1, Debug => 1);
      }
      else
      {
         $FtpObject = Net::FTP->new($ServerHost{$Host});

The problem is due to the fact that you never check for the success / failure of the call to Net::FTP::new above.

The following check should achieve what you intend and avoid the error you are seeing intermittently:

die "[Error] UNABLE TO CREATE FTP OBJECT: [$@]" if not defined $FtpObject;