LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to write a shell script to download a file via FTP? (https://www.linuxquestions.org/questions/linux-general-1/how-to-write-a-shell-script-to-download-a-file-via-ftp-268755/)

guarriman 12-21-2004 07:09 AM

How to write a shell script to download a file via FTP?
 
Hi.

I would like to automate a process to download a file from my FTP server.

Generally, I perform it by using:
$ ftp foo.com
Name> joe
Password> xxxx
ftp> cd /home/joe
ftp> get myfile.tar.gz

But I have no clue idea about how to write it with a shell script. Any idea?

AxeZ 12-21-2004 07:37 AM

#!/bin/sh
wget -c joe:xxxxx@ftp.site.com/home/joe/myfile.tar.gz

guarriman 12-21-2004 08:46 AM

wget -c joe:xxxxx@ftp.site.com/home/joe/myfile.tar.gz: Unsupported scheme. :(

When I login via FTP at ftp.site.com, I launch in /home/joe, and 'myfile.tar,gz' is
at '/home/joe/myfile.tar.g'.


SOLVED :)
wget -c ftp://joe:xxxxx@ftp.site.com/myfile.tar.gz

INCLUDE: ftp://
REMOVE: /home/joe

druuna 12-21-2004 08:58 AM

Hi,

You can use a here-document. Here's a quick example:

Code:

#!/bin/sh

HOST='some.site.com'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp $HOST <<END_SCRIPT
user $USER
$PASSWD
put $FILE
quit
END_SCRIPT
exit 0

Hope this helps.

pcunix 12-21-2004 09:31 AM

There are lots of ways to do this.

A here document as suggested above is useful, as is wget etc.

Slightly better (but only slightly) is to use a .netrc file

here's a basic .netrc (see man netrc) for normal ftp.

----$HOME/.netrc 600 perms --
machine somewhere.com login mylogin password mypass macdef
init
lcd /appl/fp/merge
cd /appx/data/50/XFR/Data
put artrx.tab TRXFER.dat
quit

machine someothermachine.org login whatever password pass macdef
init
hash
bin
prompt off

machine yetanother ...


The first example (somewhere.com) logs in, changes to a local directory /appl/fp/merge, then changes to /appx/data/50/XFR/Data on the server and "puts" a file.

With this in place, the command "ftp somewhere.com" will do the "put". You could set "prompt off" and use "mput" or "mget" in the .netrc also.

The second just logs you in to "someothermachine.org" , turns on hash, etc. and then you can type your own commands.

You can fully script more complex things with:

#!/bin/ksh
echo "machine somewhere.com login mylogin password mypass
macdef init" > $HOME/.netrc
echo "lcd /appl/fp/merge" >> $HOME/.netrc
echo "cd /appx/data/50/XFR/Data" >> $HOME/.netrc
for i in *.tab
do
echo "put $i ${i%tab}.dat" >> $HOME/.netrc
done
echo "quit" >> $HOME/.netrc
echo " " >> $HOME/.netrc
# always end a macdef with a blank line
chmod 600 $HOME/.netrc
ftp somewhere.com

Kermit has a better ftp scripting capability

http://www.columbia.edu/kermit/ftpclient.html
http://www.columbia.edu/kermit/ftpscripts.html

and so does ncftp and I think lftp


Of all the ways, I prefer Perl - see http://aplawrence.com/Unixart/perlnetftp.html for the why and the how.


--
Tony Lawrence
http://aplawrence.com


All times are GMT -5. The time now is 02:48 AM.