Solved

FTP scripting - Automate download of files and move files to an archive directory

Posted on 2012-03-30
6
2,004 Views
Last Modified: 2012-06-27
One of my clients has a connection to an Unix environment where he starts some applications wich he uses in a Windows environment.

There is also a directory wich we can access by use a NFSclient, but after 2 days we have to reboot the server, to get the connection up and running again.

There is an other option, wich is downloading the files to a local directory by using FTP.

Now i am looking for a way to download the files and i have the following:

open <IP-address>
<username>
<password>
cd ..
cd ..
cd ..
cd ..
cd ..
cd var
cd appl
cd <application name>
prompt
lcd N:\<localdirectory>
mget *.aaa
mget *.AAA
mget *.bbb
mget *.BBB
bye

Open in new window


This works when i run it, but the files are still on the remote FTP location.

What i would like to do is putting these files in the Remote FTP directory ARCHIVE for an example.

I have found a sample code on a unix / linux form
#!/bin/sh
clear
# change local directory
cd [local-directory]

#collect file names
ftp -ni ftp.abccompany.com <<EOF
user [user] [password]
cd /OUT
mls W945*.* list.txt 
quit
EOF

# create ftp action list
echo >>todo.lst user [user] [password]
while read N
do
    echo >>todo.lst cd /OUT
    echo >>todo.lst get $N
    echo >>todo.lst rename $N ARCHIVE/$N
done <list.txt

echo >>todo.lst quit

# ftp transfer process
ftp -nv ftp.abccompany.com <todo.lst

# cleanup
rm todo.lst

Open in new window


But these commands doesn't work.

Can someone help me to get this working?
0
Comment
Question by:RemcoVi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 13

Expert Comment

by:Sandy
ID: 37793229
Please use pure-ftp having upload and run after scripts.
0
 
LVL 20

Accepted Solution

by:
carlmd earned 500 total points
ID: 37795727
The following script works... use it all the time.

#!/bin/ksh
#
    HOST='remotehostname'
    USERN='anonymous'
    PASS='mm'
    cd /usr/sys/inst.images/update
    echo `ftp -inv ${HOST} >/home/ftp.como <<EndFTP
    user "${USERN}" "${PASS}"
    cd /remote/path
    binary
    mget *
    quit
    EndFTP`

NOTE that the marks before the "ftp" and after"EndFTP" are back tics. You MUST use these. I suggest you cut and past this script into vi or other editor on unix and edit it there.

remotehostname -> the path/url to the remote site you want to get files from
USERN=  -> the login name you use at the remote site
PASS=  -> the password you use at the remote site
cd /usr/....  -> this is a "cd" on you local system to where you want to put the downloaded files
echo   -> this creates a file in the path /home/ftp.como (change as you wish) that contains all that goes by in the download,
                  this is valuable if you have any problems or just want to see what happens
user   -> this logs you in on the remote system (don't change this)
cd /remote/path  -> change this to be the path on the remote site where you want to get the files from
binary -> this indicates the download is a binary file, if files are ascii you can omit this line, but it does not hurt anyway
mget * -> get all the files in the directory you cd'ed to. If you wanted all files that started with "text" then make this mget text*

Save this script with your changes and make sure it is executable. (chmod a+x script).

You can run this script without using a terminal session by using the "at" command
For example:
        at now today /path/myscript             will run the script in the background now
        at 8 pm today /path/myscript           will run the script at 8PM today
for other option see man pages for at
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 37797289
To get the files and then move them to another directory, try this:
#!/bin/ksh
#
SRCHOST=remoteHost
USER=MySelf
PASS=WhoCares
SRCDIR=/remote/file/dir
ARCDIR=/remote/file/archive
TGTDIR=/local/file/path

cd $TGTDIR

#get list of file names to transfer
ftpcmd=`
ftp -ni $SRCHOST <<EOF | grep -v ^ftp | xargs -i? echo "get "?"\nrename "?" $ARCDIR/"?
user $USER pass $PASS
cd $SRCDIR
ls -1  *.aaa
ls -1  *.AAA
ls -1  *.bbb
ls -1  *.BBB
bye
EOF`

# Execute the file transfer
ftp -ni $SRCHOST <<EOF
user $USER pass $PASS
cd $SRCDIR
$ftpcmd
bye
EOF

Open in new window

:p
0
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 

Author Comment

by:RemcoVi
ID: 37799775
Problem is that these scripts are for running on a unix environment, but i am connecting to a Unix environment, not running one.

I need to start the command on a Windows 2008 server, wich then connects to the ftp running on a Unix environment.

I tried the above anyway, but i can't get it to work.
0
 
LVL 20

Assisted Solution

by:carlmd
carlmd earned 500 total points
ID: 37800129
Sorry, the fact that you wanted to run this from Windows was not clear.

Take a look at:

http://support.microsoft.com/kb/96269

and

http://support.microsoft.com/kb/555976

and

http://www.howtogeek.com/howto/windows/how-to-automate-ftp-uploads-from-the-windows-command-line/

one of these should answer your question
0
 

Author Closing Comment

by:RemcoVi
ID: 37979291
I just bought a software license for a specific product wich makes scripting easier on FTP and it also has the option to do the things i want.
0

Featured Post

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!

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

624 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