FTP sharing tricks

The following shows you how to run a script of your choice whenever a new file is uploaded.

  1. Enable detailed FTP logging. Edit the file /System/Library/LaunchDaemons/ftp.plist, and add another ‘-l‘ option to the FTP daemon (yes, there will be two):

    ...
    <key>ProgramArguments</key>
    <array>
    <string>ftpd</string>
    <string>-l</string>
    <string>-l</string>
    <string>-n</string>
    </array>
    ...

  2. Start FTP sharing. Open ‘System Preferences‘, click on ‘Sharing‘, then select ‘File Sharing‘ from the list. Check the ‘on‘ option, then click the ‘Options…‘ button and check ‘Share files and folders using ftp‘.
  3. At this point you should be able to open a FTP session, upload a file, and have everything logged. Have a look in /var/log/ftp.log and make sure everything is logged. You should see something like:

    ...
    Jan 5 06:27:49 gandalf ftpd[23214]: ANONYMOUS FTP LOGIN FROM ::1, blah (class: guest, type: GUEST)
    Jan 5 06:27:54 gandalf ftpd[23214]: put /test.txt = 6 bytes in 0.007 seconds
    Jan 5 06:27:56 gandalf ftpd[23214]: Data traffic: 6 bytes in 1 file
    Jan 5 06:27:56 gandalf ftpd[23214]: Total traffic: 743 bytes in 1 transfer
    ...

  4. Now, let’s write a little bash script that will read the FTPD log, and will act when a new upload arrives. Save the following as a run_on_upload.sh, and customize with your own code:

    #!/bin/bash
    tail -n 0 -f /var/log/ftp.log |
    grep --line-buffered --only-matching "put /.*=" |
    while read put file delimiter; do
        # do something useful
        echo ${file:1}
    done
  5. Run the script, upload something, and see that it works… 🙂
Advertisements
FTP sharing tricks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s