Tutorial: FTP Automated Shell Scripts

March 22, 2017 | Views: 4304

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

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 -x

if [ $# != 6 ]; then
echo "Usage
./Parser.sh IP USERNAME PASSWORD SOURCE FILENAME DESTINATION
where
IP ------------- IP address of remote server
USERNAME-------- username with which to establish ftp session
PASSWORD-------- password for the above mentioned USERNAME
SOURCE --------- source directory
FILENAME ------- name of the file to be ftped
DESTINATION----- path at remote directory at which file is to be kept"
exit
fi

HOME=/home/cybrary/Documents/OUT
DIRECTORY=$4
LOG=$HOME/LOG
EXPECTED_RETURN="221 Goodbye."
LIST_OF_FILES=list_of_files
cd $DIRECTORY
ls -lrt $5* >$LIST_OF_FILES

cat $LIST_OF_FILES|while read LINE
do
filename=`echo $LINE|awk '{print $9}'`
size=`echo $LINE|awk '{print $5}'`
if [ "$filename" = "" ]; then
filename=temp

elif [ "$filename" = "_" ]; then
filename=temp

else
echo "Transfering $filename"
sh $HOME/upload.sh $filename $1 $2 $3 $6 >$LOG/ftp_log
RETURN=`fgrep "221 Goodbye." $LOG/ftp_log`

if [ "$RETURN" = "${EXPECTED_RETURN}" ]

then
mv $DIRECTORY/$filename $DIRECTORY/FTPED/$filename
else
echo "Unable to establish connection"
fi

fi
done

CODE: Upload.sh

FILE=$1
IP=$2
USER=$3
PASS=$4
DESTINATION=$5
#cd OUTPUT/CIRCLE2
ftp -inv $IP<< !EOF
user $USER $PASS
cd $DESTINATION
prompt
asc
put $FILE Temp_$FILE
rename Temp_$FILE $FILE
quit
!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.

Share with Friends
FacebookTwitterLinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterLinkedInEmail
Ready to share your knowledge and expertise?
2 Comments
  1. I didn’t test it to work on SFTP, but the way to work must be similar. Maybe you have the insert the working sockets for ssh and include the port 22 to work well. By now, these shell scripts are the right way to work with regular FTP, I will find the way to work with SFTP with SSH encryption.

  2. While this is great in concept for local traffic only, my biggest concern would be using non encrypted ftp services. Does your script support sftp? SSH2 is the current encryption standard for sftp encryption. If it’s just some coding that needs to be updated then great 🙂

Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge

 
Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel