1834780 Members
2521 Online
110070 Solutions
New Discussion

Automated ftp question

 
Adam Prince
Occasional Contributor

Automated ftp question

G'day all,
I have wrote a basic automated ftp script and I would like to test a couple of more things but I am not sure how. I would like to test if the file the are trying to get or put exists, I would also like to test if the user was able to login on the ftp server sucessfully, does anyone have any idea how I would go about doing this? Thanks for you help in advance.
Adam
8 REPLIES 8
Ian Dennison_1
Honored Contributor

Re: Automated ftp question

Quick and dirty way - Try to perform the action (get or put), then grep for the appropriate keywords in the log file that results? grep "Connection OK", grep "succesful", etc.

Can't think of any damage that could result if one of the commands didn't work - can anyone else?

Share and Enjoy! Ian
Building a dumber user
Jeff Machols
Esteemed Contributor

Re: Automated ftp question

Adam,

I don't think there is any control login within ftp. The only thing I can think of would be to execute you ftp script in debug, send that to a log file, then parse the log for the conditions you are looking for. It should be easy to search for login errors, or non-existant files.
Mark Greene_1
Honored Contributor

Re: Automated ftp question

Here's a bit from a shell script I use. The [FTP LOG FILE HERE] is where you will have to put the name of the file to which you are redirecting the ftp output on your system:

TMP_FILE=/tmp/ftperrs.$$

ERRS=`egrep "^ 425|^ 426|^ 450|^ 451|^ 452|^ 500|^ 501|^ 502|^ 503|^
504|^ 530|^ 532|^ 550|^ 551|^ 552|^ 553|ERROR|not
received|Unknown host|Login incorrect|Permission denied|transfer
aborted|Connection timed out|Unable to connect|no data for|" [FTP
LOGFILE HERE]|grep -v " bytes "`

if [ -n "${ERRS}" ]; then

for LINE in `echo $ERRS`; do

echo "$LINE" >>$TMP_FILE

done

else

echo "There were no FTP errors" >$TMP_FILE

fi

--
mark
the future will be a lot like now, only later
Krishna Prasad
Trusted Contributor

Re: Automated ftp question

What I have done in the past is after I attempted my put/get. I issued the following ftp command connected.

ls /search_dir/filename local_filename
Then when I exit ftp grep for the file name in the local file created.
Positive Results requires Positive Thinking
A. Clay Stephenson
Acclaimed Contributor

Re: Automated ftp question

Hi Adam:

In the past, I used to do this stuff in the shell but now I NEVER do because I have found a much cleaner way to do it that make error trapping duck soup. Do this stuff in Perl using the Net::FTP module which is available from http://www.cpan.org .

Here's how simple it can be:

#!/usr/bin/perl -w

use Net::FTP;
use strict;

my $ftp = Net::FTP->new("remotehost",Debug => 0);
$ftp->login("cstephen","top_secret");
$ftp->cwd("/tmp");
$ftp->get("myfile");
my $stat = $ftp->status;
my $full_stat = $ftp->code;
# $stat contains the first digit; usually all
# that you need to do is test if it is equal
# to 2. $full_stat contains the full 3-digit
# value but is seldom needed
printf("Status: %d Full Status: %d\n",$stat,$full_stat);
# Sample Test
if ($stat == 2)
{
print "Get was good\n";
}
else
{
print "Get was bad\n";
}
$ftp->quit;

I think if you start using this module you will never go back to shell scripting FTP. Moreover, these same scripts will run in the NT world.

Food for thought, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Automated ftp question

Hi again Adam:

I should have also mentioned that you need to man ftd for the meanings of the numerical FTP status codes. Again, in almost all cases, all you really need is the first digit; '2' - Good which is returned by the $ftp->status method.
If it ain't broke, I can fix that.
Deepak Extross
Honored Contributor

Re: Automated ftp question

Hi,
I'm attaching a script I had written sometime back for this purpose.
It transfers all files from a specified directory on a source m/c to a specified directory on the destination m/c.
After the ftp session terminates, it checks the file listing to ensure that all files are completely copied. If not, it retries the ftp.
Maybe you can pick up some hints from this script.
Adam Prince
Occasional Contributor

Re: Automated ftp question

Well Guys thanks for all the input. I think the problem is my lack of shell scripting experience. This is my first shell script I had to write on my own. So a lot of the ideas or helpful examples went right over my head. Thanks
again for your help.