1847738 Members
4291 Online
104013 Solutions
New Discussion

Re: FTP Question

 
ROSS HANSON
Regular Advisor

FTP Question

I need to ftp and transfer files onto another box in a shell script. How do I do the ftp portion of it?

Thank you
Ross Hanson
4 REPLIES 4
Uday_S_Ankolekar
Honored Contributor

Re: FTP Question


#!/bin/ksh
ftp -n << FTP
user
bin
prompt
mput *
FTP


-USA..

Good Luck..
Paul Sperry
Honored Contributor

Re: FTP Question

I use the following to push out the hosts file
any time I need to update it

# /usr/bin/ksh
for x in `more /home/root/list`
do
echo CONNECTING TO $x
ftp -n <<-EOF
open $x
user
cd /etc
put /etc/hosts
chmod 644 /etc/hosts
quit
EOF
done
Byron Myers
Trusted Contributor

Re: FTP Question

Here is an example of the ftp portion:

ftp -i -v $TARGET_HOST <<-/* >$FTPLOG 2>&1
put $FILE $TARGET_DIRECTORY/$FILE (REPLACE
quit
/*

you will need to create a ".netrc" file on the target machine's user's home directory (the user running the ftp script). See the man page on netrc. example .netrc for user named goofy:
machine target_host login goofy password funny
The UNIX user name "goofy" has a password of "funny". The perms on .netrc MUST be 400.

The ftp log file is in "$FTPLOG"
Just keep doing "put"s in the /* /* construct
above and end the ftp session with the "quit" above. You will want to do some preliminary checks like ping the target server to see if it is alive, and process the $FTPLOG for errors after the script is run.

If you can focus your eyes far and straight enough ahead of yourself, you can see the back of your head.
A. Clay Stephenson
Acclaimed Contributor

Re: FTP Question

I see a glaring flaw in all these responses. How do you know that the transfer was good? The better way to do this is using the Net::FTP module of Perl. You will need to download and install the Net::FTP module from www.perl.com/CPAN but the good news is that this same Perl script will run on virtually any platform including Windows.

Use if like this:

ftp.pl file1 file2 file3
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "FTP ok"
else
echo "FTP Bad; result ${STAT}"
fi

It will automatically log you in and transfer the files for you and actually attempt to repeat a failed xfer.


I never try to do FTP stuff in the shell anymore; the Perl solution just works too well.

If it ain't broke, I can fix that.