views:

382

answers:

2

Hi i have to transfer files whose names consists of two variables X, Y and they are in the directory ABC in ftp server to my local unix directory XYZ. after transfering files i have to go to local directory path and i should untar (input files are compressed files) them.

one more thing i have to use username and pwd for connecting to ftp. and copying files to local server also i have to use my username and pwd.

everything should done automatically with unix script.

Help me...plz

ftp -n hostname <<EOF
user username pwd
cd ABC
get ls *X*.tar | ls *Y*.tar username1@pwd1 : XYZ
EOF
bye
for next in `ls *.tar`
do
tar -zvxf $next
done

will it work?

thanks in advance

+1  A: 

I would suggest you just look into the manual of ftp command line ftp-tool and script with that.

Alternative: use wget to download the ftp-file to local machine, then scp to target machine, I suppose using public-key-authentication for ssh, that scp does not need a password, then it should end up simple like this.

wget --ftp-user=$USERNAME --ftp-password=$PASSWORD ftp://$HOSTNAME/ABC/$Y.tar
scp $Y.tar $SCPUSER@$SCPHOST/targetpath/$X.tar
BeowulfOF
can u see my code and tell me will it work? plz
musicking123
well, i guess not, you try to send the ftp server your scp-command.
BeowulfOF
+1  A: 

Please try below code. Hope this helps you.

#! /bin/bash

cd local_path
USER='username'
PASSWD='password'
file_name='files'

for HOST in ftpserver
do
echo $HOST
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
bin
prompt

cd "remote_path"
lcd "local_path"

mget $file_name.gz*

quit
END_SCRIPT
done

#extract file
mkdir -p ../archive/$DATE
for HOST in ftpserver
do
        gunzip $file_name.gz
done
Space
but 1. i have different usernames and pwds for local and ftp server.2. i have to select files whose names should contain X and Y
musicking123
you should investigate a little bit on yourself, if you have different usernames for ftp and ssh, just add another variable for it.
BeowulfOF
@musicking123: 1) You said you have diff user and pass for local and ftp. In my code you have to put ftp user and pass, no need for local user/pass. 2) You can use *X*.tar and *Y*.tar while mget.
Space
smal doubt why echo $HOST?
musicking123
Just to inform that ftp is running against $host if you are running this for more then one host. If you dont want, you can simply remove/comment this.
Space
first for loop didnt end with done ....is it perfect?
musicking123
As I remember the golden rules of shell scripting no script should provide output while execution if not explicitly switched by user e.g. via -v switch. No good style to provide informative output on unix shells.
BeowulfOF
I have changed the code and added done.
Space