FTP and SFTP functionality

Hi Friends,

I need to make a Unix script, where i need ftp and sftp functionality.
Let me describe in details:

I need to import few files from remote server, now these remote server either support ftp or sftp not both. So i need a script where my script will try to do ftp first and if it gets succeeded then come out of script and if not then try to do sftp on that server.

Hope i briefed properly, if not please let me know what other information requires. Please help me on this.

TIA:b:

Have you started something? show us what you have tried and where you stuck.

yes i tried few things,

for ftp:

 ftp -i -n -v ${FTPIP} <<-EOFtp >>${PREPLOG} 2>&1
        user $LOGINID $PASSWD
        cd $LOCALPATH
        binary
        mget *.* 
        dir * $TMPDIR/local.out
        bye

this is working fine but i am getting problem in sftp..please find below the sftp code

BASEDIR=/home/orozcog
DOWNLOADDIR=${BASEDIR}/ORC
LOGFILE=${BASEDIR}/download_upload_orc_data.log
[ -f ${LOGFILE} ] || touch ${LOGFILE} || error "unable to create the logfile '${LOGFILE}'"
log '=[ Starting Execution ]========================================================='


SFTP_USER='abc'
SFTP_PASSFILE=${BASEDIR}/xyz.txt
SFTP_HOST=sftp.exapmle.com
commercial_file=PPS_Commercial_Data.TXT

cd ${DOWNLOADDIR}
sshpass -f ${SFTP_PASSFILE} sftp -v ${SFTP_USER}@${SFTP_HOST} <<END_OF_SESSION >> ${LOGFILE} 2>&1
get ${consumer_file}
get ${commercial_file}
bye 
END_OF_SESSION
Exit_State=$?
[ ${Exit_State} -eq 0 ] || echo "SFTP return error ${Exit_State}"

Also i tried these two codes individually, do i need to take care of something while using ftp and sftp together, or it will just work fine if ill add IF condition

Thank You,

Getting what problem?

I thought that (condition) || ELSECASE || ELSECASE2 doesnt work.
To me, this line looks like that but tries to achieve something diffrent..

[ -f ${LOGFILE} ] || touch ${LOGFILE} || error "unable to create the logfile '${LOGFILE}'"

If file doesnt exist you want to create it, and if creation fails print the message, either way, my linux doesnt know the command 'error'.

If that is true, i'd change to:

[ ! -f ${LOGFILE} ] && (touch ${LOGFILE} || error "unable to create the logfile '${LOGFILE}'")

Note the (), wich executes the code in a 'subshell' and it wont report "unable to create file" if file already exists.

Either way, if touch cant create a file, it'll report itself...
Example: (changed error to echo)

[sea@localhost ~]$ LOGFILE=/root/log
[sea@localhost ~]$ [ ! -f ${LOGFILE} ] && (touch ${LOGFILE} || echo "unable to create the logfile '${LOGFILE}'")
touch: cannot touch '/root/log': Permission denied
unable to create the logfile '/root/log'

Hope this helps

EDIT:
Also, your EOFtp misses the 2nd entry which closes the command.
But i assume that's a copy-paste error :wink:

i tried below script:

log=/opt/app/vertica/cdr/tmp/hpcom/sftp.txt
LOGINID=abc
HOST=sftp2.xyz.com
echo "starting ftp & sftp...." > $log
ftp -i -n -v $HOST <<-EOFtp >$log
user $LOGINID $PASSWD
bye
EOFtp
rc=$?
if [[ $rc = 0 ]]; then
echo "Successful ftp...$rc" `date "+%Y-%m-%d-%H.%M.%S"` >> $log
else
sftp  $LOGINID@$HOST<<EOF > $log
#cd $DIR
#get $FILE
bye
EOF
rc1=$?
if [[ $rc1 != 0 ]]; then
    echo "Error occured...$rc" `date "+%Y-%m-%d-%H.%M.%S"` >> $log
else
 echo "Successful sftp...$rc" `date "+%Y-%m-%d-%H.%M.%S"` >> $log
fi
fi

In above code my script is not working as it should actually...below is the output of log file

"Not connected.
Successful ftp...0 2014-01-30-10.03.06"

It clearly shows that ftp is not happening on this host so ideally it shuold try doing sftp but its not coming to sftp syntax rather its coming out of script...

Please suggest where i am going wrong..
TIA:b:

sftp is in an 'else' clause, so when ftp 'succeeds' it doesn't bother.

ftp ought to be returning failure here, but must be buggy. Try capturing its output with 2>logfile and processing that file.

You supply a variable which is empty ($PASSWD).

You set the RC=$? 'too late' = its not working after EOF parts...
Because, the Return code comes from the successfull ended EOF, not the last command that was executed inside that EOF section.
Thus it'll report the wrong result..

hth

i tried using 2>logfile

in log file i am getting following details:

ftp: connect: Connection timed out
Not connected.
Successful ftp...0 2014-01-31-09.15.33

Now in my code i want to go to sftp because ftp not working but its not going, probable reason would be the rc value is coming 0 as "not connected is string not numerical value " so not able to negate the first if condition...

can anyone suggest work around please

TIA

---------- Post updated at 04:25 AM ---------- Previous update was at 04:21 AM ----------

Hi,
SO can i put

rc=$?
if [[ $rc -eq 0 ]]; then
echo "Successful ftp...$rc" `date "+%Y-%m-%d-%H.%M.%S"` >> $log

after ftp syntax...like this

ftp -i -n -v $HOST <<-EOFtp >$log 2>&1
if [[ $rc -eq 0 ]]; then
echo "Successful ftp...$rc" `date "+%Y-%m-%d-%H.%M.%S"` >> $log

well i dont think so i will be able to execute it in once i enter ftp...is there any alernative