Solved

UNIX Shell Script to Transfer Files via FTP

Posted on 2007-11-15
11
1,240 Views
Last Modified: 2014-02-21
When i execute the script I get up to here as shown below
Connected to xxxx.com.
220 xxx  FTP server (Version 1.1.214.4(PHNE_34544) Fri Feb 24 15:0
3:24 GMT 2006) ready.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>

Its not automatically logging in and puttin the files to the remote server
LOCALDIR=/backup

REMOTESERVER=servername

REMOTEPATH=/backup

LOGIN=xxxx

PASSWORD=xxxx
 

cd $LOCALDIR

ftp -n $REMOTESERVER << INPUT_END

quote user $LOGIN

quote pass $PASSWORD

cd $REMOTEPATH

prompt off

mput * devid.*

exit

Open in new window

0
Comment
Question by:port_blair2001
11 Comments
 
LVL 3

Expert Comment

by:fseto
ID: 20290790
Why not use expect or curl?  http://expect.nist.gov/  http://curl.haxx.se/docs/manpage.html
Doing it your way is rather error prone
0
 
LVL 86

Accepted Solution

by:
jkr earned 168 total points
ID: 20290913
Check out http://www.inlumineconsulting.com:8080/website/scripting.ftp.html ("How to use ftp in a shell script"). The idea is to

#!/bin/sh
HOST='ftp.users.qwest.net'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 166 total points
ID: 20290970
Hi,

To automate ftp within a script, use .netrc file in the user's home directory (the one who will run the script). This file should not be readable by others i.e. use

chmod 400 .netrc

The entry in .netrc should be as below:

machine server2
login remoteusername
password mypassword

server2 is the server which is the ftp server where you want to get the file from
remoteusername is the remote user login name on the ftp server
mypassword is the password of the remote user on the ftp server

for more info about .netrc, please use man netrc

The script could be like:

cd /dir
ftp server2 << END
cd /dir1
prompt off
get file
END

In the above script you may replace get with put if you want to send file to the remote server

For more info about ftp commands, please use

man ftp
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20291017
Hi,

One more comment, it is not a good practice to put passwords in scripts. Please avoid whenever possible.

If you want a more secure way to transfer files, then consider using sftp / scp (you need ssh installed on the other server and sftp / scp on your side).
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 40

Expert Comment

by:evilrix
ID: 20292100
Consider using the LFTP client. It is very easy to script it.

http://en.wikipedia.org/wiki/Lftp
0
 
LVL 61

Expert Comment

by:gheist
ID: 20300595
I'd suggest ncftp client which contains ncftpget/ncftpput which act in similar manner to rcp/scp, password stored no questions or interaction asked.
0
 
LVL 7

Expert Comment

by:karunamoorthy
ID: 20426060
Hello port_blair2001,
you can try this
http://www.experts-exchange.com/OS/Unix/Q_21903096.html
from
Karunamoorthy.
0
 
LVL 13

Expert Comment

by:Sandy
ID: 39278615
use PureFTP, this doesn't require any script

SA
0
 
LVL 1

Assisted Solution

by:SixBit
SixBit earned 166 total points
ID: 39350149
port_blair2001,
   1) The statement "Its not automatically logging in and puttin the files to the remote server" is not 100% true.  The script may not be putting files on the remote server, but it *is* logging in, otherwise you would not have seen the messages:

> Remote system type is UNIX.
> Using binary mode to transfer files.

The above messages only show up *after* a successful login.

2) Secondly, there's a logic error in your script at:

> 7: cd $LOCALDIR

You should be checking the shell return code from the "cd" command, otherwise you cannot know if that directory path actually exists (or is mounted), that there's no spelling mistakes in the path, and also that the process which runs this actually has SEARCH (a.k.a. EXECUTE) permission on $LOCALDIR.  This could be the sole reason the files did not arrive at the remote destination, if the "cd" failed before the FTP started...

3) Thirdly, it appears you have a typographical error in your script at:

> 13: mput * devid.*
 
you have an embedded space character between the first "*" and the partial file name "devid".  I presume you intended to use the construct "*devid.*" instead.  As written, your script would 1st attempt to send *all* files ("*"), then afterwards would try to send any files having a prefix of "devid." - which would, at that point, be completely redundant since *all* files would have been sent via the initial "*" by itself.

4) Fourthly, you're using an invalid command for FTP at:

> 14: exit

The word "exit" has no special meaning to an FTP client.  Instead, you should be using "quit" or "bye" to terminate the FTP session and exit the FTP client command mode.

5) Lastly, you've created an incomplete shell script 'here document' syntax by using:

> 8: ftp -n $REMOTESERVER << INPUT_END

but you did not have (or show us) the matching INPUT_END tag construct at the beginning of a line on a line by itself.  This should be present after an FTP 'quit' or 'bye' command [which are also missing (as per my #4 above)].

If you correct all of these issues, you may well find that your script completes the transfer successfully, however, I'd also recommend you end your script with an "exit $?" command so that the status of the FTP can be checked by any external script which has invoked this one.

SixBit

P.S.  I see it's been 5+ years since you asked this... hopefully you got resolution to this a lonnnnng time ago!  ;-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

#Citrix #Citrix Netscaler #HTTP Compression #Load Balance
Don’t let your business fall victim to the coming apocalypse – use our Survival Guide for the Fax Apocalypse to identify the risks and signs of zombie fax activities at your business.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now