Monday, June 10, 2013

Shell script to FTP to a server by passing authentication through the script

Ever struggled on writing shell script for uploading or downloading files from FTP server. Here you go with a simple and tidy shell script.


#!/bin/sh
HOST='ftp.linuxperlscripting.blogspot.in'           #FTP server to be connected
FTPUSER='Username'           #Username for the FTP account
FTPPASSWD='Password'           #Password for the FTP account
UPLOADFILE='Uploadfile.txt'           #File to be uploaded
DOWNLOADFILE='Downloadfile.txt'           #File to be downloaded
ftp -n $HOST <<END_SCRIPT           #Connect to FTP server -n option Restrains ftp from attempting auto-login upon initial connection.
quote USER $FTPUSER           #Passing ftp Username
quote PASS $FTPPASSWD            #Passing FTP Password
put $UPLOADFILE            #put is a FTP command to upload the file
get $DOWNLOADFILE           #put is a FTP command to download the file
quit #Quits the FTP session
END_SCRIPT
exit 0




Another method you can use is making use of .netrc which would be explained later