Ready to Start Your Career?

Tutorial: FTP Automated Shell Scripts

Bmsr256 's profile image

By: Bmsr256

March 22, 2017

In this post, I will show you some Unix shell scripts to automatically work with FTP servers. These are scripts that FTP to a remote server and then close the FTP connection to that server when the file has been transferred.These are the shellcodes for those three script files.CODE: Execute_FTP.sh
./Parser.sh 10.132.207.2 username password /home/cybrary/Documents/OUT DiskAlert.txt /tmp/Alert/VDS
CODE: Parser.sh
set -xif [ $# != 6 ]; thenecho "Usage./Parser.sh IP USERNAME PASSWORD SOURCE FILENAME DESTINATIONwhereIP ------------- IP address of remote serverUSERNAME-------- username with which to establish ftp sessionPASSWORD-------- password for the above mentioned USERNAMESOURCE --------- source directoryFILENAME ------- name of the file to be ftpedDESTINATION----- path at remote directory at which file is to be kept"exitfiHOME=/home/cybrary/Documents/OUTDIRECTORY=$4LOG=$HOME/LOGEXPECTED_RETURN="221 Goodbye."LIST_OF_FILES=list_of_filescd $DIRECTORYls -lrt $5* >$LIST_OF_FILEScat $LIST_OF_FILES|while read LINEdofilename=`echo $LINE|awk '{print $9}'`size=`echo $LINE|awk '{print $5}'`if [ "$filename" = "" ]; thenfilename=tempelif [ "$filename" = "_" ]; thenfilename=tempelseecho "Transfering $filename"sh $HOME/upload.sh $filename $1 $2 $3 $6 >$LOG/ftp_logRETURN=`fgrep "221 Goodbye." $LOG/ftp_log`if [ "$RETURN" = "${EXPECTED_RETURN}" ]thenmv $DIRECTORY/$filename $DIRECTORY/FTPED/$filenameelseecho "Unable to establish connection"fifidone
CODE: Upload.sh
FILE=$1IP=$2USER=$3PASS=$4DESTINATION=$5#cd OUTPUT/CIRCLE2ftp -inv $IP<< !EOFuser $USER $PASScd $DESTINATIONpromptascput $FILE Temp_$FILErename Temp_$FILE $FILEquit!EOF
 Execute_FTP.sh: The main script that will carry out the functionality of FTP service.Parser.sh: This file holds the IP, the Username, the Password, the Source and the Filename.Upload.sh: The FTP Environment Commands.I hope these script files can help you. If you have some questions or you want to fix or upgrade these scripts, you can contact me.
Schedule Demo