Solved

UNIX Shell Script to Transfer Files via FTP

Posted on 2007-11-15
11
1,261 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 
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 62

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
I had an issue with InstallShield not being able to use Computer Browser service on Windows Server 2012. Here is the solution I found.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

777 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