Solved

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

Posted on 2012-03-30
6
1,732 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
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now