LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   ftp through shell script (https://www.linuxquestions.org/questions/programming-9/ftp-through-shell-script-525150/)

HSN 02-03-2007 12:14 AM

ftp through shell script
 
I have made a research through the net on how can I write a basic shell script to automate file transfer using FTP. What I need is just to connect to a server and send/recieve a file.

What I have found is:

#!/bin/sh
HOST='host'
USER=’user'
PASS=’pass'
FILE=’file’
ftp -n $HOST < quote USER $USER
quote PASS $PASS
get $FILE
quit
exit 0



When I run the script, I get the following:
./test.sh: quote: cannot open
./test.sh: quote: not found


First, why this error appears to me?
Second, how the -n option in ftp works?

Thanks for your replies in advance.

wjevans_7d1@yahoo.co 02-03-2007 01:41 AM

The -n switch instructs ftp not to automatically try to log in when you ftp. This gives you the chance to log in using an alternative way; your shell script is attempting that alternative way.

You seem to have a few syntax errors in your script. Try this:
Code:

#!/bin/sh
HOST='host'
USER=’user'
PASS=’pass'
FILE=’file’
ftp -n $HOST <<EOD
quote USER $USER
quote PASS $PASS
get $FILE
quit
EOD
exit 0

For more information, try these commands:
Code:

man ftp
man bash


druuna 02-03-2007 06:58 AM

Hi,

The quoting is also incorrect (variable declaration). Backticks and single quotes are used, they should all be single (or double) quotes. I.e.:

Code:

#!/bin/sh
HOST='host'
USER='user'
PASS='pass'
FILE='file'

ftp -n $HOST <<EOD
quote USER $USER
quote PASS $PASS
get $FILE
quit
EOD
exit 0


HSN 02-04-2007 01:53 AM

Still, it's not working.

I modified the script as you suggested. But when I run it, nothing happens. It looks like going into an infinite loop!

druuna 02-04-2007 05:49 AM

Hi,

You say that nothing happens. Do you mean that the script does not stop? A loop is not possible, but it can hang (or is waiting for user input).

It could be that you need to change/add a few things, depending on the ftp server you are connecting to.

Things you can try:

- Add passive between quote PASS $PASS and get $FILE,
- Add binary before get $FILE (only needed if you are trying to ftp a binary file),
- Add the -v option to see what is happening (ftp -n -v $HOST).

A few questions if the above doesn't help:

1) Can you post the complete script you are using (except for the user and password, don't edit anything).
2) Is it possible to ftp from the command line using the host/user etc from the file?
3) Are you running linux or unix.
4) What is the output when the -v option is added.

Hope this gets you going again.

jlliagre 02-04-2007 06:08 AM

or better, use wget.

matthewg42 02-04-2007 06:09 AM

Quote:

Originally Posted by HSN
Still, it's not working.

I modified the script as you suggested. But when I run it, nothing happens. It looks like going into an infinite loop!

HSN, please post what you have. Copy-paste it to make sure you're showing us exactly. Of course, scrub sensitive stuff like passwords.

colucix 02-04-2007 07:12 AM

This code works fine for me:
Code:

#!/bin/sh
HOST="host"
USER="user"
PASS="pass"
FILE="file"
ftp -ni $HOST << EOS
user $USER $PASS
binary
get $FILE
bye
EOS

Just a note: in the codes above 'quit' is not a ftp command! Probably this was the reason of "infinite loop". Not really a loop, as druuna stated, but simply a wait status for a valid command!

jschiwal 02-04-2007 08:00 AM

I think that colucix nailed it.

I had to rename a large set of files (around 3000) in around 50 devices that had ftp access. Doing it manually would be no good. So instead I retrieved the directory, used sed to convert it into a script and called it like:
ftp user:pass@host <script

There were many devices to loop through, and I found I needed to add the 'bye' command at the end of the script for it to exit.

As a side note, this doesn't work as advertised if you are using cygwin.

HSN 02-05-2007 05:04 AM

Actually the code is very simple and I have modified it according to what I'm reading in this thread:

Code:

#!/bin/sh

HOST="10.11.12.18"
USER="oracle"
PASS="----"


ftp -n $HOST <<EOD
quote USER $USER
quote PASS $PASS


cd dba
get test.txt
quit
EOD
exit 0


wjevans_7d1@yahoo.co 02-05-2007 06:12 AM

So are you saying that it works now, or that it still hangs?

HSN 02-05-2007 11:41 PM

No, as I said it hangs.

colucix 02-06-2007 12:18 AM

Have you tried the ftp directly from the command line? Does it works? Anyway, why don't you try the bye command instead of quit? Bye is the native command to terminate a session, and it is recognized from all versions of ftp, while quit is not accepted from some of them.

jlliagre 02-06-2007 12:39 AM

Again: why aren't you using wget ?

HSN 02-06-2007 08:43 AM

Yes, actually I'm using ftp itself everyday to manually pull some backup files. That's why I'm willing to write a script for that.
Ok.. I'll try bye command instead.


jlliagre
what benefit do I obtain from wget?


All times are GMT -5. The time now is 08:42 AM.