Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

ftp-watch.pl

by TVSET (Chaplain)
on Apr 29, 2003 at 19:35 UTC ( [id://254069] : sourcecode . print w/replies, xml ) Need Help??
Category: FTP stuff
Author/Contact Info Leonid Mamtchenkov aka TVSET (leonid@leonid.maks.net)
Description: Little thingy to watch over directory listing changes on the remoted FTP host. Spits out the differences in GNU diff format. All listings are stored in files. Works recursively down from the top directory specified in settings.
#!/usr/bin/perl -w

=head1 NAME

ftp-watch.pl - Simple reporting for updates on remote FTP server.

=head1 DESCRIPTION

When run first time, this script generates a file with recursive listi
+ng
of some directory on remote FTP server.  Any other script execution wi
+ll
cause it to report differences in directory listing using GNU diff for
+mat.

=cut

use Text::Diff;
use Net::FTP::Recursive;
use strict;

#
# Settings
# 
my $host = 'ftp.linux.dom';         # Server
my $user = 'anonymous';             # Username
my $pass = 'me@here.com';           # Password
my $dir  = '/pub/people/leonid/';   # Top directory to monitor
my $listing_file = '/tmp/ftp.real'; # Keep listing in this file
my $listing_temp = '/tmp/ftp.temp'; # Temporary file

# Get FTP listing
open (OUT, ">$listing_temp") or die "Couldn't open $listing_temp ($@)"
+;
my $ftp = new Net::FTP::Recursive($host) or die "Couldn't connect ($@)
+";;
$ftp->login($user,$pass) or die "Couldn't login ($@)";
$ftp->cwd($dir) or die "Couldn't change directory ($@)";
$ftp->rls(Filehandle=>*OUT);
$ftp->quit();
close (OUT);

# Create empty data file if does not exist
if (! -e $listing_file) {
    warn "No local listing...creating empty";
    open (OUT, ">$listing_file") or die "Couldn't open $listing_file($
+@)";
    close (OUT);
}

# Compare
open (IN1, "<$listing_file") or die "Couldn't open $listing_file ($@)"
+;
open (IN2, "<$listing_temp") or die "Couldn't open $listing_temp ($@)"
+;
my $diff = diff \*IN1, \*IN2;
close (IN2);
close (IN1);

# Clean-up
rename ($listing_temp, $listing_file) or warn "Couldn't update info ($
+@)";

# Output results
print $diff,"\n";

=head1 AUTHOR

Leonid Mamtchenkov <leonid@leonid.maks.net>

=cut
Replies are listed 'Best First'.
Re: ftp-watch.pl
by hardburn (Abbot) on Apr 30, 2003 at 14:13 UTC

    I used to always download the latest Linux kernel when it was announced on Slashdot. Ever since I stopped reading Slashdot regularly, I've been missing new releases. I've been meaning to write something like this for a while now. Thanks for posting it.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated