Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1348
  • Last Modified:

UNIX Shell Script to Transfer Files via FTP

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
port_blair2001
Asked:
port_blair2001
3 Solutions
 
fsetoCommented:
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
 
jkrCommented:
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
 
omarfaridCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
omarfaridCommented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
Consider using the LFTP client. It is very easy to script it.

http://en.wikipedia.org/wiki/Lftp
0
 
gheistCommented:
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
 
karunamoorthyCommented:
Hello port_blair2001,
you can try this
http://www.experts-exchange.com/OS/Unix/Q_21903096.html
from
Karunamoorthy.
0
 
SandyCommented:
use PureFTP, this doesn't require any script

SA
0
 
SixBitCommented:
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now