1846732 Members
3988 Online
110256 Solutions
New Discussion

Re: batch ftp

 
Johan Nielsen
Advisor

batch ftp

I know this question has been asked before but i didn't find it in the archives. I'm working on setting up a scripted ftp session and I've tried the

ftp << EOF
user
password
ls
quit
EOF

and I've tried placing the commands in a file and using it as input to the session and in both cases it gets hung up on the password prompt.

Any ideas?

Thanks
Johan
8 REPLIES 8
A. Clay Stephenson
Acclaimed Contributor

Re: batch ftp

I'll give you my standard answer:

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. You even get error trapping for free. 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("somehost",Debug => 0);
$ftp->login("cstephen","TopSecret");
$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 OK\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 also run in the NT world. You can download a free version of perl for windows at http://www.activeperl.com.
Note that the answer is now the same on UNIX and NT.


Notice that this method easily handles the error checking. If you like, you can use the shell for most of your script and simply use a bit a perl for the actual FTP transfers. In that case add the statement exit($stat) to the perl script and then your shell script does have a valid status indication. You only need Perl on the FTP client end.

It also becomes trivially easy to add looping to the code to retry the put/get a number of times if you get a staus of other than 2.


Regards, Clay
If it ain't broke, I can fix that.
Bill Costigan
Honored Contributor

Re: batch ftp

Use the .netrc file to supply the user and password for ftp.

you can supply a user and password for ech system to which you ftp. Once you get past the logon the rest of your script should work.

the ./netrc file goes in your home directory.

I use this to get patches from HP
machine ftp.itrc.hp.com login anonymous password me@my-company.com
S.K. Chan
Honored Contributor

Re: batch ftp

Try this .. create an executable file call "ftp-test" and in it put ..
#!/usr/bin/sh
{ echo "user username password
ls
quit" ; } ftp -i -n hostname
Put the appropriate "username", "password" and "hostname" in place and run it like so ..
$ ./ftp-test
Of course .. Clay's approach highly recommended.
harry d brown jr
Honored Contributor

Re: batch ftp


Johan,

What was wrong with Clay's answer?

perl is really the best solution!

live free or die
harry
Live Free or Die
Johan Nielsen
Advisor

Re: batch ftp

Actually there was nothing wrong with the answer. My understanding is that you rate the answer based on how well or quickly the problem is resolved.

I believe that the perl solution is probably the best one but the quickest resolution came from utilizing .netrc file.

I will adjust my rating accordingly in the future.

Sorry Clay.

johan
Evert Ladrak
Advisor

Re: batch ftp

I know it's a bit late but the way to do without the .netrc file is as follows

ftp -n <user password
..
..
quit
EOF

I agree the perl solution is better but sometimes shell is the quicker solution

-ETL
Evert
harry d brown jr
Honored Contributor

Re: batch ftp

Johan,

Good, because I like Clay's answer. 10 = best answer, 9-8 = very very good, 7-5 = good, 4-1 = not exactly, 0 = way off base.

live free or die
harry
Live Free or Die
Fred Martin_1
Valued Contributor

Re: batch ftp

I like a shell solution because I do most things in shell; perl is fine if you use perl often. So something like this is very readable if you know ftp, and therefore easy to understand:

# ftp to NT server, get the backup report
#
(
echo "open 192.1.1.67"
echo "user anonymous root-corp"
echo "ascii"
echo "get checkfab.txt"
echo "delete checkfab.txt"
echo "bye"
) | ftp -i -n

fmartin@applicatorssales.com