Issue with ftp shell script

I'm trying to set up a shell script that will ftp a file from another server. Here is the code I have currently:



exec 4>&1
ftp -nv >&4 2>&4 |&

print -p open $HOST
print -p user $USER $PASSWD
print -p cd /home/ollie/scripts/backup 
print -p binary
#print -p hash
print -p prompt
print -p lcd /home/opike/scripts/backup
print -p mget *.gz
print -p bye

exit 0

Open in new window

This script works some of the time and fails at other times. When it fails, it returns the following output:
Connected to testdataload.
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
331 Please specify the password.
230 Login successful.
250 Directory successfully changed.
200 Switching to Binary mode.
Interactive mode off.
Local directory now /home/opike/scripts/backup
221 Goodbye.

The mget *.gz command is just skipped over without any error message being displayed.

When I interactively run the ftp command line client and enter the commands one by one, it always works... and that seems to "unclog" something because right after that I will go back and run the script, and bingo, it works again. But then a few hours later it starts failing again....

This is on CentOS 5.2 btw.
Who is Participating?
Do you have lftp on the system? While this isn't a ksh solution, I've found this has helped me in the past:

lftp -u ${USER},${PASSWD} ${HOST} <<EoF
cd /home/ollie/scripts/backup
lcd /home/opike/scripts/backup
mget *.gz

Open in new window

@dalewalker111 writes the ftp commands using the HERE document. This ensures that the commands are not executed until the login process is finished, so my suggestion is, even if you don't have lftp, to rewrite your script to be similar to dalewalker111's.
opikeAuthor Commented:
@rbride - Not sure what you mean by the "HERE document".
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

print -p mget *.gz

Will there always be a .gz file?

My guess is when you run the script, sometime the gz file is not there yet; so it directly return.

opikeAuthor Commented:

I've verified that the .gz file is there. Plus, without changing anything on the server side, if I manually ftp from the client and enter in the commands by hand, it works.
The HERE document is where you get the input from the script inline.

You wrote in one part <<EOF

well that's an example of using the HERE document.
See for example:
opikeAuthor Commented:
Your script works great. Thanks.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.