Wednesday, December 3, 2008

FTP Using a Shell Script

Manually transferring a file or files via FTP is a common and convenient method for moving data from one computer to another, especially if it’s a non-recurring event. But the reality is there are many times when an event is recurring and calls for immediate automation. This can be done by using a simple UNIX script file, which can then be executed via command line interface or added to the crontab. In the shell script, myftp.sh, example below, I'm FTP’ing binary type files (pictures) from a local computer to a remote while logging the activity.

Note: Some organizational policies may not allow login/password information in a script file.

# vi myftp.sh
#! /bin/sh
REMOTE='esoft'
USER='anyuser'
PASSWORD='myftp125'
FTPLOG='/tmp/ftplog'
date >> $FTPLOG

ftp -n $REMOTE <<_FTP>>$FTPLOG
quote USER $USER
quote PASS $PASSWORD
bin
cd /myraid/dailyjpgs
mput *.jpg
quit
_FTP
:wq!


Run via CLI
# ./myftp.sh

Add it to the crontab
# crontab -e

Alternate post: FTP Using One-Liner and Perl Script

Per commenter's request: (call login and password outside of script)

# cat > /export/ftp/.user.txt
anyuser
control+d

# cat > /export/ftp/.passwd.txt
myftp125
control+d

# vi myftp.sh
#! /bin/sh
REMOTE='esoft'
USER=`cat /export/ftp/.user.txt`
PASSWORD=`cat /export/ftp/.passwd.txt`
FTPLOG='/tmp/ftplog'
date >> $FTPLOG

ftp -n $REMOTE <<_FTP>>$FTPLOG
quote USER $USER
quote PASS $PASSWORD
bin cd /myraid/dailyjpgs
mput *.jpg
quit
_FTP
:wq!

No comments: