View Single Post

   
  #4 (permalink)  
Old 04-24-2008, 04:43 PM
ThreeStar
 
Posts: n/a
Default Re: Scripted FTP transfer

On Apr 22, 2:34 am, "Julian Brett" <nos...@nospamhere.com> wrote:
> 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.


Sounds like a problem with the command file (/tmp/ftp.$$). Post
that. From the "Not connected" errors I'm guessing it's a problem
with establishing user credentials. Are you able to FTP interactively
to the host from this box?

BTW if this script is designed to run in a particular context I'd
use .netrc files to contain the user name and password instead of
echoing them in the script. It's somewhat more secure, makes the
password easier to change, and simplifies your script.

Better yet is SCP, but you didn't ask about that.

R Robert
*** Software
Reply With Quote