Friday, June 17, 2011

Log an FTP Session with a Filter

A couple colleagues and I were looking for an effective way to tersely log the activities of an FTP effort. All we wanted was the file name and throughput rate. If you are Ftping hundreds or thousands of files, the first one (below) could be quite verbose, especially if you have a security banner notice. Here are a couple ideas that we considered. We went with the alternative (a lot less text-based overhead).

#!/bin/sh
ftp -nv some_host /tmp/myftplog ‹‹EOFTP
user some_user some_password
cd /some_dir/some_dir1/some_dir2
binary
put some_image.jpg
EOFTP

and its log...

$ more /tmp/myftplog
Connected to some_host.
220 ProFTPD x.x.x Server ready.
331 Password required for some_user
230 User some_user logged in
250 CWD command successful
200 Type set to I
200 PORT command successful
150 Opening BINARY mode data connection for some_image.jpg
226 Transfer complete
ftp: 26753 bytes sent in 0.00Seconds 26753000.00Kbytes/sec.
221 Goodbye.

###ALTERNATIVE###

#!/bin/sh
ftp -n some_host /tmp/myftplog ‹‹EOFTP
user some_user some_password
cd /some_dir/some_dir1/some_dir2
binary
verbose
put some_image.jpg
EOFTP

and its log...

$ more /tmp/myftplog
Verbose mode On .
200 PORT command successful
150 Opening BINARY mode data connection for some_image.jpg
226 Transfer complete
ftp: 26753 bytes sent in 0.00Seconds 26753000.00Kbytes/sec.
221 Goodbye.

Display it...

$ tail -f /tmp/myftplog

1 comment:

Brian Mathis said...

Not to sound gruff, but who scripts FTP like that anymore? Using redirects is highly error prone, does not take into account potential problems, and is a relic of the 80's.

It's trivially easy to perform FTP scripting using Perl, Python, or most other scripting languages, and handle error conditions, etc... Even if you don't know the language, copy/pasting a script from the man pages usually gets you everything you need to know.