Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Net::FTP output to array

by packetstormer (Monk)
on Aug 18, 2011 at 08:59 UTC ( [id://920892]=perlquestion: print w/replies, xml ) Need Help??

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

Hello

I am running Net::FTP via a CGI script and while it all works fine the module doesn't return any output to screen to show the user the job ran ok. I have enabled debug=>1 and it dumps the output to the screen when running from the terminal.

Does anyone know how to either redirect a function to an array/string or catch the Net::FTP to one? Code below:

#!/usr/bin/perl use strict; use warnings; use CGI; use Net::FTP; use Expect; my $query = new CGI; my ( $template_type, $logged_in, $cookie ) = get_template( { template_name => "uploadbackup.tmpl", query => $query, type => "locked", permission => { allowed_upload => '*' }, debug => 1, } ); my $query = new CGI; my $server_ip = $query->param('server_ip'); my $server_user = $query->param('server_user'); my $server_pass = $query->param('server_pass'); my $os = $query->param('os'); my $file_to_upload = $query->param('file_to_upload'); # Check the Offsite server input boxes have been filled if( (length($server_ip)==0) || (length($server_user) ==0) || ( +length($server_pass) == 0) ) {my $invalid_server_ip = "Nope - you need to fill all +the boxes!."; $template_type->param('invalid_server_ip' => $invalid +_server_ip); } else { my $ftp_message = upload_file($server_ip,$server_user,$server_pass +,$file_to_upload,$os); $template_type->param('file_to_upload' => $file_to_upload, 'ftp_message' => $ftp_message ); } output_html_with_http_headers $query, $cookie, $template_type->output; sub upload_file { my $login_failed_message = "Login Failed. Please check your us +ername and/or password"; my $ftp_message; my $os = $_[4]; my $ftp = Net::FTP->new($_[0], Debug => 1) or die "Cannot conn +ect to $_[0]"; $ftp->login($_[1],$_[2]) or die $login_failed_message; if($os eq "1") { $ftp->binary(); } $ftp->put($_[3]) or die "get failed ", $ftp->message; $ftp->quit; return $ftp_message; }

Replies are listed 'Best First'.
Re: Net::FTP output to array
by Perlbotics (Archbishop) on Aug 18, 2011 at 09:31 UTC

    my $ftp = Net::FTP->new($_[0], Debug => 1) or die "Cannot connect to $ +_[0]"; $ftp->login($_[1],$_[2]) or die $login_failed_message; ... $ftp->put($_[3]) or die "get failed ", $ftp->message;
    Probably one of the many die()'s triggered? You could wrap the call to upload_file() with an eval block:
    my $ftp_message = 'n/a'; eval { $ftp_message = upload_file($server_ip,$server_user,$server_pass,$fil +e_to_upload,$os); }; if ($@) { # catched an exception $ftp_message = "Error: $@"; ... } else { ... }

    Update: Oops, misread the question (but you still need to handle exceptions).

    One possibility is to hook into the Net::Cmd::debug_print() method, which performs sending the debug messages to STDERR:

    { # temporarily change Net::Cmd::debug_print my $log_buffer; local *Net::Cmd::debug_print = sub { my ($self, $dir, $text) = @_; $log_buffer .= ($dir ? 'snd --> ' : 'rcv <-- ') . $text; }; my $ftp = Net::FTP->new($_[0], Debug => 1) or die "Cannot connect to + $_[0]"; ... $ftp->quit; print "Log:\n$log_buffer\n"; } # recovers previous behaviour of Net::Cmd::debug_print

      Hey - that works perfectly. Thanks! I have spent a lot of time on this. Really appreciate it!
      Thanks for the reply. However, the script runs correctly and does it's job. The problem I am having is catching it's output.
Re: Net::FTP output to array
by Khen1950fx (Canon) on Aug 18, 2011 at 10:55 UTC
    I'll let you straighten the CGI problems out, but to address the Net::FTP issues, here's a simplified CGI ftp script that might give you some ideas:
    #!/usr/bin/perl -T use strict; use warnings; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use Net::FTP; $| = 1; my $host = 'ftp.cpan.org'; my $user = 'anonymous'; my $pass = 'anonymous'; my $ftp = Net::FTP->new($host, Debug => 2); $ftp->login($user, $pass); $ftp->cwd('/pub/CPAN/modules') or die; $ftp->binary; my @files = $ftp->ls or die; print h2 "\n\tlisting files...\n\n"; for (@files) { print p $_, "\n"; }
      Thanks but my FTP is working fine, unless you noticed something wrong?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://920892]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-24 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found