LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-03-2007, 12:14 AM   #1
HSN
LQ Newbie
 
Registered: Dec 2006
Distribution: Ubuntu
Posts: 25

Rep: Reputation: 15
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.
 
Old 02-03-2007, 01:41 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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

Last edited by wjevans_7d1@yahoo.co; 02-03-2007 at 01:44 AM.
 
Old 02-03-2007, 06:58 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
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
 
Old 02-04-2007, 01:53 AM   #4
HSN
LQ Newbie
 
Registered: Dec 2006
Distribution: Ubuntu
Posts: 25

Original Poster
Rep: Reputation: 15
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!
 
Old 02-04-2007, 05:49 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
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.
 
Old 02-04-2007, 06:08 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,492

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
or better, use wget.
 
Old 02-04-2007, 06:09 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.
 
Old 02-04-2007, 07:12 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,489

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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!

Last edited by colucix; 02-04-2007 at 07:14 AM.
 
Old 02-04-2007, 08:00 AM   #9
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
Old 02-05-2007, 05:04 AM   #10
HSN
LQ Newbie
 
Registered: Dec 2006
Distribution: Ubuntu
Posts: 25

Original Poster
Rep: Reputation: 15
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
 
Old 02-05-2007, 06:12 AM   #11
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
So are you saying that it works now, or that it still hangs?
 
Old 02-05-2007, 11:41 PM   #12
HSN
LQ Newbie
 
Registered: Dec 2006
Distribution: Ubuntu
Posts: 25

Original Poster
Rep: Reputation: 15
No, as I said it hangs.
 
Old 02-06-2007, 12:18 AM   #13
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,489

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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.
 
Old 02-06-2007, 12:39 AM   #14
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,492

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Again: why aren't you using wget ?
 
Old 02-06-2007, 08:43 AM   #15
HSN
LQ Newbie
 
Registered: Dec 2006
Distribution: Ubuntu
Posts: 25

Original Poster
Rep: Reputation: 15
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto ftp shell script mcrosby Linux - Newbie 9 06-27-2007 06:09 AM
shell script to transfer file from ftp bbgtilak Linux - General 1 12-27-2006 07:35 AM
shell script for ftp javier_ccs Programming 7 07-18-2006 11:02 PM
Automating FTP upload via a shell script? Spitty Programming 5 01-16-2006 10:28 PM
Trouble when automating ftp with shell script hari_s_82 Linux - Newbie 2 09-23-2004 02:17 AM


All times are GMT -5. The time now is 08:28 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration