Help with perl NET::ftp script (109 Views)
Reply
Trusted Contributor
Juan M Leon
Posts: 285
Registered: ‎01-14-2003
Message 1 of 1 (109 Views)

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;
      }
   }

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation