0

I have a linux box (Debian) which is up 24/7. I'd like to set it up in such a way, that once a day it connects to a specific FTP server and checks if any files have changed since the last check. If they have, it should send me an email with a list of the changed files.

The check doesn't have to be thorough - I'll be happy if it just compares file dates. But it has to be recursive.

How can I do this? I understand I can use cron to schedule the process, but what do I use to connect and check for changes?

5 Answers 5

1

The ncftp client has the ncftpls tool which can do a recursive long list.

1
  • I'm looking at that too. Actually, I suppose this is what would solve the problem... if I had not solved it in another way. Still, this question is valid in itself. :)
    – Vilx-
    Jun 18, 2009 at 11:17
1

Here is a Perl solution using Net::FTP. The below script should print the filename and timestamp for each file in the fileserver. You can easily extend this to suit your actual needs.

#!/usr/bin/perl

use strict;
use Net::FTP;
use File::Listing qw(parse_dir);
use POSIX qw(strftime);

# Configuration Options
my $FTP_SERVER        = 'ftp_host_name';
my $FTP_USER          = 'username';
my $FTP_PASS          = 'password';

# Establish FTP connection
my $ftp = Net::FTP->new( $FTP_SERVER, Debug => 0 )
  or die "Cannot connect to $FTP_SERVER: $@";
$ftp->login( $FTP_USER, $FTP_PASS )
  or die "Cannot login ", $ftp->message;
$ftp->binary;

# Get list of files
my $files = $ftp->dir;
foreach my $entry (parse_dir($files)) {
    my ($fileName, $type, $size, $mtime, $mode) = @$entry;
    next unless $type eq 'f';
    my $timeStamp = strftime "%Y-%m-%d %H:%M:%S", gmtime($mtime);
    print "Name: $fileName\t Time Stamp: $timeStamp\n";
}

$ftp->quit;
0

I'm pretty sure lftp can be scripted to do this; otherwise, I'd just use an FTP library for my favourite scripting language (Ruby/Perl/Python) and do the job that way.

1
  • I had an idea of [some FTP folder listing software here, ncftp perhaps?] -> diff -> grep (filters out a few folders I don't care about) -> email.
    – Vilx-
    Jun 18, 2009 at 9:21
0

Here is a shell script to help you on your way. If you can execute this over ssh, then you're done. If you have to do it over FTP, then you'll be meeting my old friend Expect. Good luck!

#!/usr/bin/env bash

if [ ! -f .baseline ]; then
    echo "No timestamp available for comparison. Exiting."
    exit 1
fi

for filename in ./*
do
    if [ .baseline -ot "$filename" ]; then
        echo "$filename"
    fi
done

touch .baseline

0

Use wget. First day:

wget -r -q --spider --no-remove-listing --ftp-user=YOU --ftp-password=YOURSECRET ftp://YOURSERVER   2>&1

This command will produce a local directory named YOURSERVER with a downloaded directory tree underneath, but each directory will contain only one file called ".listing". The .listing files can be compared to old copies with diff command.

Now each day:

mv YOURSERVER PREVIOUS_DIR
wget as above
diff -r YOURSERVER PREVIOUS_DIR 

This will print all lines that differ, and cron should automatically mail the result to you.

3
  • Seems a step in the right direction! Would of course be nicer if there was just one file, not a whole tree.
    – Vilx-
    Jun 18, 2009 at 9:44
  • Luckily diff -r compares nicely whole directory trees. I agree it would be better to have a FTP client supporting something like 'find . -ls' and store just one listing file locally. I am not sure why do you need the file names, but in case you want to download these files: wget has -N option (downloads newly modified/created files).
    – kubanczyk
    Jun 18, 2009 at 10:07
  • I want to know what has changed, and then decide on my own whether these files a worthy of downloading and putting in a SVN folder.
    – Vilx-
    Jun 18, 2009 at 10:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .