ganeshm69 has asked for the wisdom of the Perl Monks concerning the following question:

During FTP i need to catch the response from the Server, into a Log file. i have used NET:: FTP class, here im not able to redirect my output to a Log file. my code :
################################## $ftp = Net ::FTP->new("xxx.xxx.xxx",Timeout=>30,Debug=>1) $ftp->login($usr,$paswd) $ftp->cwd($dir) $ftp->get($filename); quit; #################################
The above code gives the server response,in the Stdout. i want to redirect this out to a LogFile. since i do this FTP process in a Loop.From the logfile, i can check whether the file has been ftp'ed properly or not in the Log file. A similar code in Shell is like
######################################### ftp -nv ${ipaddress} << !!! > ${LOGFile} user ${USER} ${PASSWD} cd ${FTPHOME} get ${File} quit !!! #############################################
here i get the log file from the FTP process into LOGFile. i would like to do the same in Perl. Could any one Help me in this Issue.

2002-10-26 Edit by Corion : Added Code tags

Try This: ganeshm69 ############################################# open( STDERR , "> FTP_LOG" ); $ftp = Net ::FTP->new("xxx.xxx.xxx",Timeout=>30,Debug=>1) $ftp->login($usr,$paswd) $ftp->cwd($dir) $ftp->get($filename); quit; close(STDERR); ############################################# #The response will be present in the log (FTP.log) file.

Replies are listed 'Best First'.
Re: Problem while FTP
by Mr. Muskrat (Canon) on Oct 26, 2002 at 13:16 UTC

    According to the documentation for Net::Cmd (which is used when you set Debug to something other than 0), it says that the output is sent to STDERR. Why not just redirect STDERR to a log file from within your script? Or you could do so on the command line.

Re: Problem while FTP
by vek (Prior) on Oct 26, 2002 at 16:55 UTC
    Redirecting STDOUT to a file:
    % your_ftp_prog.pl > your_log_file
    Redirecting STDERR to a file:
    % your_ftp_prog.pl 2> your_log_file
    -- vek --
      Or, to do that from within the perl script (since some shells don't support redirection of just STDERR):
      open(STDOUT, ">$my_log_file"); open(STDERR, ">$my_err_file");
      and of course you can double the angle brackets to append to the logs, rather than overwriting them on each run.
Re: Problem while FTP
by TStanley (Canon) on Oct 27, 2002 at 02:37 UTC
    A quick search on CPAN turned up these modules which may be of some use.

    TStanley
    --------
    It is God's job to forgive Osama Bin Laden. It is our job to arrange the meeting -- General Norman Schwartzkopf
Re: Problem while FTP
by converter (Priest) on Oct 27, 2002 at 14:18 UTC

    Net::FTP inherits from Net::Cmd. As with any module that inherits from other modules, it's a good idea to check the documentation for all base class modules (the modules listed in the @ISA array, if there is one) when you're learning how to use a particular module. Perl uses the modules listed in @ISA to find methods that are invoked against an object of a particular class when those methods aren't found in the object's class.

    Looking at line 26 of Net::FTP (v. 2.62), we see that Net::FTP inherits methods from Exporter, Net::Cmd, and IO::Socket::INET. There's probably not much of interest in the Exporter documentation, but Net::Cmd's documentation lists a method called message that:

    Returns the text message returned from the last command

    You should be able retrieve the server response for all of the commands you're sending to the server with methods invoked against your $ftp object like so:

    open LOG, ">> $LOGFile" or die "IO error opening $LOGFile for append: +$!"; ... $ftp->get($filename); print LOG "[get $filename] server response: ", $ftp->response || 'no r +esponse', "\n";

    I hope that helps.