Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use ftp in shell script or program

Status
Not open for further replies.

MatthewP

Programmer
Jan 16, 2001
176
GB
I can get ftp working from the command line like so :

ftp -nv << eod
open <myaddress>
user <username> <password>
get <filename>
quit
eod

but if i put this in a shell script I get unknown host and unknown hostrb.net when they're exactly the same. Surely it must be possible to ftp through a shell script?

Thanks,
Matt.
 
Sure, but not if you just put those lines in a script.

From
How can I automate an ftp transfer?

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

* *
You can also use &quot;here&quot; files:

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


Finally, ftp already has a scripting language built in to it. &quot;ncftp&quot; (available from Skunkware) has 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 &quot;puts&quot; a file.

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

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

You can fully script more complex things with:

#!/bin/ksh
echo &quot;machine somewhere.com login mylogin password mypass
macdef init&quot; > $HOME/.netrc
echo &quot;lcd /appl/fp/merge&quot; >> $HOME/.netrc
echo &quot;cd /appx/data/50/XFR/Data&quot; >> $HOME/.netrc
for i in *.tab
do
echo &quot;put $i ${i%tab}.dat&quot; >> $HOME/.netrc
done
echo &quot;quit&quot; >> $HOME/.netrc
echo &quot; &quot; >> $HOME/.netrc
# always end a macdef with a blank line
chmod 600 $HOME/.netrc
ftp somewhere.com
Tony Lawrence
SCO Unix/Linux Resources tony@pcunix.com
 
expect utility is simple to control a console program in background, although &quot;eod&quot; reminds me of &quot;FE&quot; where scp from openssh will be more appropriate
 
Thanks for your help. I'm new to this shell scripting lark - still trying to find a good book explaining the whole linux thing with all the ins and outs...
Matt.
 
If you are someone who learns by examples, may I suggest:

&quot;Linux Shells by Example&quot; by Ellie Quigley
ISBN: 0130147117 Bruce Garlock
bruceg@tiac.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top