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

Dear Monks, I found this code snippet on this site today and I was wondering how I can capture what the FTP ouputs on the screen. It prints it all the output to the screen, but I can't store it in an array, because I am printing to the filehandle and not reading from it.
if ($var eq "Y") { open(FTP,'| ftp -i -v -n ') or die "Couldn't run FTP: $!\n"; my $ofh = select(FTP); $|=1; select($ofh); print FTP "open $FTP_HOST\n"; print FTP "quote USER $FTP_USER\n"; print FTP "quote PASS $FTP_PASS\n"; print FTP "cd $FTP_DIR\n"; print FTP "bin\n"; print FTP "get $FTP_FILE\n"; print FTP "bye\n";
Thanks

Replies are listed 'Best First'.
Re: Capturing FTP output
by moritz (Cardinal) on May 15, 2008 at 12:16 UTC
      Is this what you mean? Cannot get it too work
      my $pid = open2(\*READ, \*WRITE, "ftp -i -v -n" ); print WRITE "open $FTP_HOST\n"; print WRITE "quote USER $FTP_USER\n"; print WRITE "quote PASS $FTP_PASS\n"; print WRITE "cd $FTP_DIR\n"; print WRITE "FTP "bin\n"; print WRITE "get $FTP_FILE\n"; print WRITE "bye\n"; my @data = <READ> ;

        Your script as posted contains won't compile as it contains syntax errors. Your big problem, however, is that ftp, when not in interactive mode, doesn't write to anything to stdout. You won't know if anything goes wrong. Ftp will just exit without saying anything. So you'll need to check your files to see if the transfer actually took place.

        Caveat: tested on FreeBSD.


        s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: Capturing FTP output
by derby (Abbot) on May 15, 2008 at 14:52 UTC

    I must've missed something. Why would you use the system ftp (and IPC wrappers) when Net::FTP is available? Do you have a really old version of perl? (When did libnet make it into the core?)

    -derby
      [canker:~]$ corelist Net::FTP Net::FTP was first released with perl 5.007003 [canker:~]$