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 you can even check if the sizes of remote and local-file are equal (only 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 not 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 not 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; }