#!usr/bin/perl/ # # Created by Doney Jimenez, dznet.com, on 04-02-2001 # Use as is # Use at your own risk # I, Doney Jimenez, am not responsible for any damages this program/script may cause # # Details # This program will download files from a given ftp site. # The files downloaded will be saved at a local directory. # This script is to be use as a server to monitor and download files # from your ftp site on the internet. It will download your files as soon as they # are evailable in your ftp site. # Security of your files will increase because your files will be deleted # from your ftp site as soon as they are downloaded to your LAN/computer. # This script works based in usernames and password. It wont search the entire directory tree of # the ftp site. # Needed modules can be found at cpan.org # This script was developed for WINDOWS, it will run in LINUX with a few modifications. # # Variables Setup # There are two variable to setup # %USER_NAMES, has your ftp usernames and password # $host, has your ftp site domain name # # # Updates # 04-09-2001 Fix, do not download files being uploaded # # # # use Socket; use Strict; use Win32::Internet; use LWP::Simple; use HTML::Parse; use HTML::Element; use URI::URL; $I = new Win32::Internet(); while () { # run forever, infinite loop ########### Timer ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); #$REAL_TIME = $hour . $min . $sec; print "FTP file downloader\n"; print " The time is: ",$hour, ":", $min, ":", $sec, " Waiting for 5 minutes before continuing\n\n"; sleep 300; # Whait for these many seconds before each round ########### This are the user names and passwords in your ftp site, You can have one or more %USER_NAMES = ''; %USER_NAMES = ("username1","password1", "username2","password2", "username3","password3", "username4","password4", "username5","password5"); ########### Download the files if any $result = ''; foreach $key (keys %USER_NAMES) { $key =~ s/^\s+//; $key =~ s/\s+$//; $USER_NAMES{$key} =~ s/^\s+//; $USER_NAMES{$key} =~ s/\s+$//; #print "Getting file from FTP site \n\n"; $host = "ftp_domain.com"; $user = $key; $pass = $USER_NAMES{$key}; #$FILE_TO_DOWNLOAD = "xxx.prn"; print "+++++++++++++++++++++++ Now processing user: $user ++++++++++++++++++++++++\n\n"; print "Connecting to FTP site '$host'\n\n"; $FTP = ''; # flushing to prevent problems with FTP handle $I->FTP($FTP, $host, $user, $pass); if(!$FTP) { ($num, $text) = $I->Error(); print "Could not connect to ftp, ***** Error: [$num] $text\n"; sleep 5; } else { # print " FTP.handle=" . $FTP->{'handle'} . "\n"; print " Pasvmode is: ", $FTP->Pasv(), "\n"; print " Binamode is: ", $FTP->Mode(), "\n"; print "-------------------- Server said ---------------------\n"; print $FTP->GetResponse(); print "\n\n"; $path = $FTP->Pwd(); $err = $FTP->Error(); if ( $err ) { print "Error: $err\n\n"; sleep 4; } print " Current directory is '$path'\n" if ! $result; $path = $FTP->Pwd(); if($path) { print "Current directory is '$path'\n"; sleep 4; } else { $err=$FTP->Error(); print "***** Error: $err\n\n"; sleep 4; } print "Trying to download file\n\n"; print "Getting directory listing of ftp account\n"; @files = ''; @files = $FTP->List("*.*", 3); $err = $FTP->Error(); print "***** Error: $err\n\n" if ! @files; print "Found $#files files.\n"; if($#files>0) { foreach $file (@files) { if (($file->{'name'} ne ".htaccess") && ($file->{'name'} ne ".htpasswd")) { # this are system files, ignore them $size = ''; $size = $file->{'size'}; sleep 30; #$file_name = $file->{'name'}; @this_file = ''; @this_file = $FTP->List($file->{'name'}, 3); foreach $this_file (@this_file) { #$file_size = $this_file->{'size'}; #print "file1 $file->{'name'} $size ++++++++++++++++++++++++++++++++++++++++\n"; #print "file2 $this_file->{'name'} $this_file->{'size'} +++++++++++++++++++++++++++++++++++\n"; #sleep 10; if ($size eq $this_file->{'size'}) { # Make sure file is completed "not being uploaded" $file_to_download = ''; $file_to_download = $file->{'name'}; print "Now downloading: $file_to_download \n\n "; $result = $FTP->Get($file_to_download,$file_to_download); sleep 4; if($result){ print "Deleting $file_to_download\n\n"; $result = $FTP->Delete($file_to_download); $err = $FTP->Error(); print "***** Error: $err\n" if ! $result; sleep 4; } } } } } print "\n"; } #$result = $FTP->Get('database_file.txt',$FILE_TO_DOWNLOAD); $err = $FTP->Error(); print "Could not download file, ***** Error: $err\n\n" if ! $result; $FTP->Close(); print "\n\n Download completed for this user \n\n" if @files; print "\n\n No files found for this user \n\n" if !@files; sleep 5; }# Else loop finish }# Foreach loop finish print "Round completed\n"; print "The end\n\n\n\n"; }# Infinite while loop finish