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------
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|