Friday, 5 August 2011

HOW-TO: Email notification of SFTP file upload

Following on from my post, 'HOW-TO: Chrooted SFTP only access' I wanted to find a way to get an email notification of when a client had uploaded a file to my work's SFTP area. I had Sendmail already installed.

Each client has their own subdirectory under /home/sftp/ and I wanted to monitor them all and trigger an email when a new file was created.

Ok, I don't know how much use this will be to others but I am just going to throw down what I did to achieve what I wanted. These are personal notes and use them at your peril.

sudo apt-get install iwatch

installs iWatch, written in Perl and based on inotify, a file change notification system, a kernel feature that allows applications to request the monitoring of a set of files against a list of events.

sudo touch /etc/init.d/sftp-iwatch

creates an empty file. I edited the file and typed

iwatch -r -e create -c "(w;ps -ef)|/home/david/ %f '$($(which date))'" /home/sftp >> /dev/null 2>&1 &

which basically executes a script /home/david/ when a new file is created under /home/sftp/.

# 20110723 David Humble

DATE=$($(which date))
SUBJECT="File upload notification"

echo "Date: $DATE" > /var/upload.txt
echo "To: $EMAIL" >> /var/upload.txt
echo "From: $FROM">>/var/upload.txt
echo "Subject: $SUBJECT">>/var/upload.txt
echo "New file $FILE created at $CREATED on aadfiler SFTP Server.">>/var/upload.txt
echo ".">>/var/upload.txt

cat /var/upload.txt | /usr/sbin/sendmail -t

sudo chmod +x /etc/init.d/sftp-iwatch
sudo chmod +x /home/david/

makes both files executable.

cd /etc/init.d
update-rc.d sftp-iwatch defaults

Adds sftp-iwatch to the servers startup processes.

now you can manually start sftp-iwatch by typing
sudo /etc/init.d/sftp-iwatch

Now whenever a client uploads a file to any subdirectory of /home/sftp/ I get a notification thus:
Date: Wed Aug 3 16:27:02 BST 2011
From: aadfiler@xxx.local
Subject: File upload notification

New file /home/sftp/clientsname/upload/AAD013 GE eNewsletter Day 3.csv created at Sat Jul 23 13:36:37 BST 2011 on aadfiler SFTP Server.


Categories: How-To, Linux, OS, Open-SSH, SFTP server, Chrooted SFTP only access
Tags: kde, ubuntu server, Linux, open-ssh, chroot, sftp, File upload email notification, iwatch, notify, dnotify


Anonymous said...


just going through this archive post of HOW-TO: Email notification of SFTP file upload"

BTW great process !! I was looking for something like this, One thing i have noticed that every upload email alert New file created time is exactly the same each time on each upload. Do you know why its doing that ? I have check the script parameters but could fix it



Taurus II said...

Hi Sohail,

/etc/init.d/sftp-iwatch should pass two parameters to the script, the filename and the date. The date is the result of calling the system's 'date' command.

From a BASH type
'which date'
and copy the resulting absolute path into /etc/init.d/sftp-iwatch
That should resolve your issue.

I have now edited the above /etc/init.d/sftp-iwatch, to get the absolute path.