Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

5 Answers 5

up vote 1 down vote accepted

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

share|improve this answer
    
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 '09 at 11:17

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.

share|improve this answer
    
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 '09 at 9:21

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

share|improve this answer

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.

share|improve this answer
    
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 '09 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 '09 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 '09 at 10:13

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;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.