1826214 Members
3135 Online
109691 Solutions
New Discussion

Re: scripting issue

 
SOLVED
Go to solution
Ted Mims
Occasional Advisor

scripting issue

embedded within a script I have the following:

ftp server <
7 REPLIES 7
harry d brown jr
Honored Contributor

Re: scripting issue


You could look at using tcl/tk to script the session, but it's a lot to learn if you have never used it before.
Live Free or Die
Ashish Tripathi
Advisor

Re: scripting issue

Hi,
I trap the return messages like this

ret=`ftp -n -v 2>&1 <<- END
open ${SERVER}
user ${USER} ${PASSWORD}
${TYPE}
lcd ${TO_DIR}
cd ${FROM_DIR}
get ${ORIGINAL_FILENAME} {NEW_FILENAME}
bye
END`

echo "$ret" | grep -q -i "incomplete"
if [[ $? -eq 0 ]]
then
echo "Ftp failed to get files"
exit 1
fi

echo "$ret" | grep -q -i "failed"
if [[ $? -eq 0 ]]
then
echo "Ftp failed to get files"
exit 1
fi

echo "$ret" | grep -q -i "No such"
if [[ $? -eq 0 ]]
then
echo "Ftp failed to get files"
exit 1
fi

echo "$ret" | grep -q -i "denied"
if [[ $? -eq 0 ]]
then
echo "Ftp failed to get files"
exit 1
fi

echo "$ret" | grep -q -i "refused"
if [[ $? -eq 0 ]]
then
echo "Ftp failed to get files"
exit 1
fi

echo "$ret" | grep -q -i "aborted"
if [[ $? -eq 0 ]]
then
echo "Ftp failed to get files"
exit 1
echo "$ret" | grep -q -i "cannot"
if [[ $? -eq 0 ]]
then
echo "Ftp failed to get files"
exit 1
fi

echo "$ret" | grep -q -i "Not connected"
if [[ $? -eq 0 ]]
then
echo "Ftp failed to get files"
exit 1
fi


Ashish Tripathi

The best way to make dreams true is to wake up.
Alan Riggs
Honored Contributor

Re: scripting issue

You could also do:

yadda yadda yadda
put file
get file file.check
!

diff file file.check
if [ $? -ne 0 ]
then
error routine
fi

Sridhar Bhaskarla
Honored Contributor
Solution

Re: scripting issue

Try this work around

ftp -v server << EOF > /tmp/logfile 2>&1
cd directory
put file
bye
EOF
grep "Not Connected" /tmp/logfile|grep -v grep
if [ $? = 0 ]
then
echo "Error on ftp"
fi

It should work.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
harry d brown jr
Honored Contributor

Re: scripting issue

If you don't mind the overhead, you could add to your script:

get directory/file /tmp/diditwork

and then at the end of your script cksum the original and comapre that to the cksum of the temp file.
Live Free or Die
James R. Ferguson
Acclaimed Contributor

Re: scripting issue

Hi Ted:

The 'ftp' session doesn't return an exit value denoting success or failure.

If you really want to decipher success/failure you can parse the three-digit numbers included with the reply from each 'ftp' command. Redirect the ftp session's verbose output into a logfile and parse the replies. The man pages (1M) for 'ftpd' [the daemon] discusss the reply format. Essentially, the first digit of the message indicates whether the reply is good, bad, or incomplete.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: scripting issue

Hi (again) Ted:

One more comment. When analyzing ftp's replies, the format consists of a three-digit number, a space, some text, and an end-of-line.

The numbers are standard. The text will vary operating system to operating system. Hence is is safer to parse and analyze the 3-digit number in the reply.

Regards!

...JRF...