1847925 Members
4654 Online
104021 Solutions
New Discussion

Re: FTP status

 
MANISH PATEL_2
Frequent Advisor

FTP status

Hi everyone,

I am getting/putting the file from remote server then i delete the file from my server.

I want to check the status before i delete or moved the file from server. I want to check if any "Login failed" and "host not connect" error through scripts. I found that if it ftp return "221" means it exits from the ftp session wheather it is successful or "Login failed" but for "host not connect" it want display any number.

I want to check through "if" condition. Can you please tell me how i can check the condition in scripts.

Pls find the following scripts which doesn't check proprly.

T1=`grep "221 " FTPLOGOUT`
T2=`grep "Login failed" $FTPLOGOUT`
if [ "$T1" -eq "221 " ]
then
if [ "$T2" -ne "Login failed." ]
then
rm *
echo "FTP Done"
else
echo "FTP Failed : Login Failed(cbpout)" >> $MAILLOG
fi else echo "FTP failed : Host not connected(cbpout)" >> $MAILLOG
fi

Thanks
Manish
3 REPLIES 3
Rob Leadbeater
Honored Contributor

Re: FTP status

Hi Manish,

You should seriously think about rewriting your script in Perl, and use the Net::FTP module.

It will be far more stable.

http://search.cpan.org/~gbarr/libnet-1.21/Net/FTP.pm

Hope this helps,

Regards,

Rob
A. Clay Stephenson
Acclaimed Contributor

Re: FTP status

Trying to do this in the shell is very tedious and FTP's error codes mean different things at different times. It's MUCH easier to do it in Perl and you get error checking for free. The attached script, ftpput.pl, will ftp to host "mouse" as user "mickey", password "secret", cd to /aaa/bbb, set ASCII mode, try every file up to 3 times, and put file1, file2, and file3. It will even read the password from a .netrc file so that it doesn't have to be passed on the command line. You don't have to know any Perl at all. All you have to do is test the exit status. If it's 0, all went well otherwise a non-zero exit code is set and an error message is output on stderr.

typeset -i STAT=0
ftpput.pl -h mouse -l mickey -p secret \
-d /aaa/bbb -A -h 3 file1 file2 file3
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Ftp failed; status ${STAT}." >&2
fi
exit ${STAT}

Invoke as ftpput.pl -u for full usage.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: FTP status

... and here is ftpget.pl. Invoke as ftpget.pl -u for full usage. The really good news is that if you have Perl installed on a Windows box, the Perl scripts work equally well there as well as on UNIX.
If it ain't broke, I can fix that.