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? |
#!/bin/sh
wget -c joe:xxxxx@ftp.site.com/home/joe/myfile.tar.gz |
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 |
Hi,
You can use a here-document. Here's a quick example: Code:
#!/bin/sh |
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. |