1828584 Members
2465 Online
109982 Solutions
New Discussion

FTP script help

 
SOLVED
Go to solution
Anand_30
Regular Advisor

FTP script help

Hi,

Please find below the FTP script which I have written:

inp=ftp.config
file=abc
file1=def
for serv in `cat $inp`
do
echo $serv
echo "Trying to connect to $serv" > /tmp/ftp_log.${serv}
ftp -i -n <<-EOF >> /tmp/ftp_log.${serv}
open $serv
user anonymous
as
cd /tmp
prom
mget $file
quit
EOF
STAT=$?
echo $STAT
if [ ${STAT} = "0" ]; then
echo "FTP SUccessful to the server $serv" >> /tmp/ftp_log.${serv}
else
echo "There were some problems in connecting to $serv " >> /tmp/ftp_log.${serv}
fi
done

I am reading the server names from the input file and trying to connect to all the servers in the input file.

The script works good but the problem is that whenever a particular server is not reachable due to some network problem, the script times out. But my requirement is that the script should send a message to the log that it is not able to connect to a server and move on to the next server.

I have tried to use the FTP return status but it seems to be 0 even when the server is not reachable.

Is there a way in which I return an error to the FTP log file in case the file which I am trying to obtain is not availble in the server.

Also, I have to obtain '$file1' in case '$file' is not available. I don't know how to do this.

Please help.

Thanks,
Anand
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: FTP script help

Let me show you a much easier way using a Perl script which will do all the status and timeout stuff and can check for connectivity.

#!/usr/bin/sh
inp=ftp.config
file=abc
dir=/tmp
USER=anonymous

cat ${inp} | while read SERV
do
echo "Testing connection to ${SERV} \c"
ftpget.pl -h ${SERV} -l ${USER} -z
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Ok"
echo "Getting file ${file} \c"
ftpget.pl -h ${SERV} -l ${USER} -d ${dir} -B -t 3 ${file}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Ok"
else
echo "Failed to get file ${file} from host ${SERV}; status ${STAT}" >&2
else
echo "Failed to connect to ${SERV}. Status ${STAT}" >&2
fi
done


Invoke as ftpget.pl -u for full usage. Once you start using Perl for FTP, you will never use the kludgy shell scripts again.

If it ain't broke, I can fix that.
Biswajit Tripathy
Honored Contributor

Re: FTP script help

I would go with Stephenson's perl script, but if you
must use shell script, you could ping the ftp server
before doing the ftp.

ping FTPSERVER -n 1 | grep -q "100% packet loss"
if [ $? -eq 0 ]
then
echo "FTP_SERVER unreachable"
else
.... your script here
fi

Ping will timeout much quicker than ftp.

- Biswajit
:-)
Anand_30
Regular Advisor

Re: FTP script help

Hi Stephenson,

I have used your solution. It works great.

But I have a question. Sometimes I am having to fetch file say abc_${value}_*.

I understand from ftpget.pl that I have to use "-L" option when I am trying to fetch files that match some pattern.

I tried using the following command

ftpget.pl -h ${SERV} -l {USER} -p ${PASS} -A -t 3 -L 'abc_${value}_*'

It returns only the name of the file like abc_1234_98918. But it does not fetch the file.

Can you please help.

Thanks,
Anand
A. Clay Stephenson
Acclaimed Contributor

Re: FTP script help

The -L option is working as intended. It lists files matching a pattern in the -d dir. It does not do a get. After you get the list of files, you then issue another ftpget.pl command.

ftpget.pl -h remotehost -l user -p passwd -d /xxx/yyy file1 file2 file3

This will get file1, file2, and file3 from directory /xxx/yyy on the remotehost.
If it ain't broke, I can fix that.