Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: ftp return value

by kodo (Hermit)
on May 16, 2002 at 06:53 UTC ( [id://166938]=note: print w/replies, xml ) Need Help??


in reply to ftp return value

lo Raj, I've had similar problems and I've finally done it this way:

use Net::FTP; use Net::Ping; # Calling the subs...: # Checking if host is up... sub pingcheck { my $host = shift; if ($ping->ping($host)) { return 0; } else { print "$host is dead...\n"; return 1; } } # Connect to Server... sub connectftp { my $host = $_[0]; unless ($ftp = Net::FTP->new($host, Timeout => 6, Debug => 0)) { print "Couldn't connect to $host\n"; return 1; } return 0; } # Do login... sub loginftp { my ($user, $pw) = @_; unless ($ftp->login($user, $pw)) { print "Couldn't login\n"; return 1; } return 0; } # Set transfer-mode sub modeftp { my $mode = shift; if ($mode eq 'B') { $mode = "binary"; } elsif ($mode eq 'A') { $mode = "ascii"; } elsif ($mode eq 'E') { $mode = "ebcdic"; } elsif ($mode eq "Y") { $mode = "byte"; } else { $mode = "binary"; } unless ($ftp->$mode()) { print "Couldn't set transfermode to $mode!\n"; return 1; } print "$mode\n"; return 0; } # Now here we have the GET-Part, it supports *.txt also (mget), and yo +u can even check if the sizes of remote and local-file are equal (onl +y makes sense with binary data often) sub getftp { my ($file, $localfile) = @_; my ($remotesize, $localsize) = ''; my @files = (); if (! $localfile) { $file =~ /.*\/(.*)$/; $localfile .= $1; } if ($localfile =~ /\/$/) { $file =~ /.*\/(.*)$/; $localfile .= $1; + } if ($file =~ /\*/) { unless (@files = $ftp->ls($file)) { print "Couldn't download $file as $localfile: $file does n +ot exist?!\n"; return 2; } } else { push(@files, $file); } foreach $file (@files) { if (! $localfile) { ($localfile) = $file =~ /.*\/(.*)$/; } unless (@_ = $ftp->ls($file)) { print "Couldn't download $file as $localfile: $file does n +ot exist?!\n"; return 2; } unless ($ftp->get($file, $localfile)) { print "Couldn't download $file as $localfile!\n"; return 1; } if ($checksize == 1) { $localsize = ((stat($localfile))[7]); foreach($ftp->dir($file)) { ($remotesize) = $_ =~ /^.*?\s+ +.*?\s+.*?\s+.*?\s+(.*?)\s.*?/; } if ($localsize ne $remotesize) { print "Failure while downloading $file as $localfile: +Sizecheck failed ($localsize != $remotesize)!\n"; return 1; } } print "Downloaded $file\n"; } return 0; }


I hope this helps you a bit, the code can for sure be optimized but it's a pretty good way I think to check if everything went right...

Greets,

giant_

-----BEGIN PERL GEEK CODE BLOCK----- Version: 0.01 P++>+++$c-> P6 >+R+>+M+>++O >+MA+>+++E+>++PU+>+++BD C+>++D!S X!WP >+++MO?PP++n CO?PO-o+G A--OL!Ee---Ev++Eon!Eot!Eob!Eoa!uL++uB uS!uH+uo+w---m!osA-osBE- ------END PERL GEEK CODE BLOCK------

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found