Re: Scripted FTP transfer



Rather then specify the username and password in your script, I would
suggest you put the ftp destination name/IP address, user name and password
details in to a .netrc file in the home directory of the user name that is
going to run the ftp script, so....

If you are running this from root, the .netrc will be created in /

The format of .netrc would be:

machine name/IP address login username password password

set chmod to 600 for .netrc. Then run your script minus the ftpuser and
ftppass variables

Dave

"Julian Brett" <nospam@xxxxxxxxxxxxxx> wrote in message
news:fukbc8$mej$1$8300dec7@xxxxxxxxxxxxxxxxxxx
Hi all.

I'm attempting to script an FTP transfer on my UnixWare 7.1.4 server.
I've written numerous FTP transfer scripts before, but haven't really
approached it this way.

What I'd like to do is basically retrieve some files from a remote system,
then move them at the remote end into an archive folder. Sounds pretty
simple.

In my script, I'm trying to write a FTP command file on the fly to
redirect into my FTP command as follows:-

===========
:
ftphost=ftp.XXXXXX.com
ftpuser=XXXXXX
ftppass=XXXXXX
ftpdir="/"
ftparcdir="/arcdir"
work=/tmp/xfer.$$
#
# get list of files waiting
ftp -g -n ${ftphost} > ${work} 2>&1 << !
user ${ftpuser} ${ftppass}
cd ${ftpdir}
dir
bye
!

# process file list into ftp commands
exec 3< ${work}
while read line <&3
do
filename=`echo ${line} | awk '{print $4}'`
fileext=`echo ${filename} | awk -F\. '{print $2}' | dd conv=ucase
2>/dev/null`
if [ "${fileext}" = "XML" ]
then
echo "get ${filename}" >> ${work}.2
echo "ren ${filename} ${ftparcdir}/${filename}" >> ${work}.3
fi
done

# write ftp script file to retrieve all XML files
#echo "OPEN ${ftphost}" > /tmp/ftp.$$
echo "USER ${ftpuser} ${ftppass}" >> /tmp/ftp.$$
echo "binary" >> /tmp/ftp.$$
echo "cd ${ftpdir}" >> /tmp/ftp.$$
cat ${work}.2 >> /tmp/ftp.$$
cat ${work}.3 >> /tmp/ftp.$$
echo "bye" >> /tmp/ftp.$$

# run script to transfer all XML files and archive them
ftp -i -g -n ${ftphost} < /tmp/ftp.$$ > /tmp/ftp$$.log 2>&1
rm -f ${work} ${work}.2 ${work}.3

cat /tmp/ftp$$.log
rm -f /tmp/ftp$$.log
rm -f /tmp/ftp.$$

exit 0

===========

The script runs and creates my command file, but I just cannot get it to
accept the commands from the temporary file. I have removed the redirect
to the ftp$$.log file, and stripped out the relevant commands and run them
directly from the command line.

The output I get (either on the screen, or in the ftp$$.log file) is:-

?Invalid command
?Invalid command
Not connected.
Not connected.
Not connected.
Not connected.
Not connected.
Not connected.


This is the method for ftp which I have found on Google, but cannot
understand why it isn't working on my Unixware system. I get exactly the
same results on an Openserver 5.0.6 system too.

Any ideas what I'm doing wrong here?


I suppose I could also do a "mget *.xml" in this instance, but not knowing
the case of the files (it could be another Unix or Linux system at the
other end - I don't know), this may not work.



Many thanks,

Julian.



.


Quantcast