1828317 Members
3994 Online
109976 Solutions
New Discussion

How to loop an FTP?

 
SOLVED
Go to solution
Ronald Cogen
Frequent Advisor

How to loop an FTP?

Hello out there,

I want to sent large ascii files from one server to another using ftp. It would be nice if I could write a loop that would take care of this within the ftp command. I started to put together a scipt you will find in the attachment.

it's not finnished and I don't like it anyway, because it can put in only one file at a time. How can I write the script, so that it will loop, thereby taking care of all the files in the directory safely. I would also like to be able to send, let's say, 10 files at a time. A collegue of mine suggested I just use a wild card in the ftp-command, but that didn't help.

Can someone help me out here?
Regards,
Ron
I've been down so long it looks like up to me
8 REPLIES 8
Andreas Voss
Honored Contributor
Solution

Re: How to loop an FTP?

Hi,

at attachment i put you a new version of your script that would put severeal files over ftp.

Regards
Balaji N
Honored Contributor

Re: How to loop an FTP?

Hi,
The script was .doc file so couldnt open it. here is a script. i have a similar one to which i pass the server, username, passwd, files etc and it ftp's the files for me.


++++++cut here +++++++++++++++
USER=" "
PASSWD=" "
SERVER = " "
FILES = ".. .. .. .. .."
ftp -in $ << EOF
user $USER $PASSWD
mput $FILES
bye
EOF
+++++end of script ++++++++++

not tested it but should work.

hth
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Paula J Frazer-Campbell
Honored Contributor

Re: How to loop an FTP?

Hi


Use the mput command or

put
put


See man ftp


Paula
If you can spell SysAdmin then you is one - anon
John Palmer
Honored Contributor

Re: How to loop an FTP?

You can't loop within ftp itself. However, you could either:-

1. ftp each file individually within a shell loop, something like...

for file in...
do

done

2. Build a file which is used to drive ftp, something like...

cat > $TMPF << EOD

EOD

for file in...
do
cat >> $TMPF << EOD
put $file
EOD

done

ftp -n < $TMPF

Regards,
John
Yogeeraj_1
Honored Contributor

Re: How to loop an FTP?

hi,
i would use the mput option instead.

E.g.
==============================
#!/bin/sh
# YD-25/10/2002
# test1.sh
ftp gig-slx2 <cd /home/yd/myfiles
prompt
mput ydfiles*
bye
EOF
==============================

easy ;)

Cheers
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
David Lodge
Trusted Contributor

Re: How to loop an FTP?

You could also use co-processes to spawn off ftp in the background (ftp |&) and write strings to it by using 'print -p' and 'read -p' the problem with this is that you need to parse the responses properly - but this can be sorted by coding once and keeping the function library...

For example (just typed out now - not tested!)

ftp -nv foo |&

print -p "user alice"
print -p "pass bob"
do
read -p line
until ${line% *} = @(2??)
for i in ${filelist}
do
print -p "put ${i}"
done

This gives you the advantage that you can test to see whether the put actually worked and retry or report an error (but you will need to parse the return codes)

The other way is to use expect, or netcat to achieve a similar goal as above.

HTH

dave
Ronald Cogen
Frequent Advisor

Re: How to loop an FTP?

Hello Andreas,

I tested your script suggestion. It worked! I just had to change the source and destination, but that was my thing. Thanks a lot.
How does the 'echo' work? It contradicts the assumtion, which several other suggestions imply, that one can't loop within ftp!
Again thanks.

Regarding all other suggestions: I will take a look at them and credit them as they apply.
Thanks to everyone for the responses!

Ron
I've been down so long it looks like up to me
Ronald Cogen
Frequent Advisor

Re: How to loop an FTP?

Andreas,

I thought a bit more about hoe the echo works.
It seend the comman out to the shell that called the script. That's why I can see the commands being executed.

Thanks,
Ron
I've been down so long it looks like up to me