Scripting FTP Transfers

How can I automate an ftp transfer?

There are actually numerous ways to do that. If your script is complex, I'd use use Perl Net::FTP or Kermit.




You can also use "here" files fpr simple cases:


 HOST=xxx
 FTPUSER=xxx
 FTPPASSWORD=xxx
 ftp -n $HOST <<-EOF
 user $FTPUSER $FTPPASSWORD
 cd /wherever
 bin
 prompt off
 mget '*'
 bye
 EOF


Hate these ads?


 


Finally, ftp already has a somewhat primitive scripting language built in to it. "ncftp" or "lftp have even more capability, but 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/bash
 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


M3IP inc.


 




Comments


Wed Jun 20 08:33:31 2007: Subject:   anonymous
Many thanks for your examples !!! Great help for the actual task I'm working on. The only problem I get is the blank line at the end of the .netrc file.



Here is my file :

#!/bin/bash
echo "machine my.remote.server login mylogin password mypass
macdef init" > $HOME/.netrc
echo "lcd /home/postgres/scripts/activite/export" >> $HOME/.netrc
echo "cd sauve" >> $HOME/.netrc
for i in *.sql
do
echo "put $i " >> $HOME/.netrc
done
echo "quit" >> $HOME/.netrc
echo " " >> $HOME/.netrc
# always end a macdef with a blank line
chmod 600 $HOME/.netrc
ftp my.remote.server

and here is the result I'm getting :

Connected to my.remote.server.
220 ProFTPD 1.2.10 Server (ProFTPD Default Installation) [XXX.XX.XX.XX]
500 AUTH not understood
500 AUTH not understood
KERBEROS_V4 rejected as an authentication type
Macro definition missing null line terminator.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>

Any suggestion to solve this problem ?

TIA

Chris from France

Wed Jun 20 08:44:14 2007: Subject:   anonymous
Sorry !!! I should'nt have put a space character on the blank line. Now, it works great. Thanks



Chris

Fri Sep 21 06:41:57 2007: Subject: zoujt5433@yahoo.com.cn   JohnZou
i am so sorry to trouble than i make my shell like this:



#!/bin/bash
echo "machine my.remote.server login mylogin password mypass
macdef init" > $HOME/.netrc
echo "lcd /home/tempt" >> $HOME/.netrc
echo "cd test" >> $HOME/.netrc
echo "prompt" >> $HOME/.netrc
echo "mget *t" >> $HOME/.netrc
echo "quit" >> $HOME/.netrc
echo " " >> $HOME/.netrc
chmod 600 $home/.netrc
ftp 192.168.1.2

but i can not login my server.hope you can give me help soon.Thanks.here is the information like this waiting for input user and password.

Connected to 192.168.1.2
220(vsFTPd 2.0.1)
502Command not implemented
502Command not implemented
KWEBEROS_V4 rejected as an authentication type
Name (192.168.1.2:root):

Fri Jul 25 13:07:20 2008: Subject: Solution-   ljuban


Note: Just add additional machine macro i your .netrc script at the end. Like folowing:
machine dummy
login dummy
password dummy
macdef init
prompt
mput *
quit




Tue Jul 29 16:45:32 2008: Subject: FTP   EvanCarroll
http://evancarroll.com

Dynamically creating .netrc files is categorically stupid. Macdef is *only* for sites which you never intend to frequent in the ftp shell -- you should make mention of this in your tutorial. You might want to do something more akin to:

#!/usr/bin/bash
SHELL=/bin/sh ftp -i <<EOF
user
password
ftpcommands
bye
EOF

or something like that.



Tue Jul 29 16:53:22 2008: Subject:   TonyLawrence


I'll disagree strongly. I think Perl Net::FTP is a far smarter way to script transfers, but scripting .netrc files is certainly NOT stupid - but using the <<EOF absolutely is..



Tue Jul 29 22:23:37 2008: Subject: FTP   EvanCarroll
http://evancarroll.com

The heredoc that was originally given is a *good idea.* My arguement is against the writing to ~/.netrc to abuse macdef. Scripts that impliment this method will:
Be insecure if your home is mounted on a unencrypted-transit file system (exportfs).
Be insecure if anyone has write access to your home. (ex. creates a fifo which you mistakingly write to -- you don't check for it)
Fatally err if you can't write to home (ex read only fs, or the like)
The list can go on, but why should I?

The shell here-doc method works in most any shell, and doesn't require appending to a configuration file -- which by definition is not supposed to be dynamic anyway.

In other news, Net::FTP isn't a program, it is library, which isn't half bad or good, but requires an understanding of perl. Ckermit has a restrictive license and is hardly warranted for just FTP use. ncftp on the other hand is a totally cross-platform scriptable application for ftp, which while a farily stagnant works well. Non x-platform solutions you might be interested in would be curlfs or any other fuse based abstraction of ftp. In summary, dynamically appending to netrc is an all around bad soluation.

Consider simply echoing the script to ftp via stdin, rather than the *stupid* netrc voodoo the commenter is trying.





Tue Jul 29 22:27:08 2008: Subject:   TonyLawrence


I flatly disagree.

"Here" files often cause hung sessions - .netrc files do not. If you are worried about security, deal with it.



Tue Jul 29 22:33:20 2008: Subject:   TonyLawrence


By the way, do you know your website resolves to 127.0.0.1 ???



Tue Jul 29 22:52:59 2008: Subject: FTP   EvanCarroll
http://evancarroll.com

lol, good one. If you file a bug report I'll be more than happy to take a look at it for you and see about resolving it. There is no reason one of these should hang while the other one doesn't. You might consider checking that you have the '-i' flag (so you never wait for input), and that your shell isn't obscenely old. Use, ltrace, and strace, to narrow down where it is hanging.

My web site resolves to 127.0.0.1 because of a court order to have it removed.

Tue Jul 29 22:58:11 2008: Subject:   TonyLawrence


I wonder what that says about you...

But regardless: knowing that it doesn't exist, why advertise it??

And you think using .netrc is stupid :-)

Tue Jul 29 23:17:59 2008: Subject: FTP   EvanCarroll
evancarroll.com

.netrc isn't stupid, is is just being abused via macdef. I use macdef for the reason it it is there, to bootstrap every /bin/ftp session with a macro. I have some some servers where the ftp homedir is permed -rwx, and you have to blindly chdir someplace that has perms that work for you. In this case .netrc is very handy.

re: evancarroll.com: it will exist again, soon enough, when I win the case.

Tue Jul 29 23:21:18 2008: Subject:   TonyLawrence


Well, best of luck with your case and we'll just continue to disagree about this.

Add your comments

Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner


LOD Communications, Inc.

Views for this page
Today This Week This Month This Year  Overall
2338237,488 12,752

/Unixart/scripting_ftp.html copyright 1997-2003 (various) All Rights Reserved

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

More:
       - FAQ




Unix/Linux Consultants


http://thatitguy.com Business networking servers, Linux and Unix experts. In business since 1997! Windows and Exchange to Samba and Scalix migration experts.


http://www.breakthru.com.au SCO (Openserver and Unixware), Unix, Solaris and Linux Consulting services including: Secure Networking Solutions; Linux based Firewalls; Backup Solutions; Secure Home to Office Network Setup; Phone, Remote and On-Site Support available - Satisfaction Guaranteed!


http://echo3.net/ Unix/Linux Custom Applications, Web Hosting, C/C++ Programming Courses



Twitter
  • Nov 30 20:25
    I have 37,000 words of a 50,000 word project. I'd like to finish it this week..
  • Nov 30 20:05
    My wife made turkey sandwiches with stuffing and cranberry orange relish - I did not want to eat the last bite. Didn't want it to end!




card_image








Change Congress


Related Posts