SFTP Shell script

Hi All,

I have done the Private - Public keys generation( and sharing) and have written a script for automating the SFTP. Need to make sure if it will work:

sftp.sh
#!/bin/bash 
. config.ini
sftp $SFTP_USER@$SERVER <<EOF 
cd $SFTP_RDIR
#ls -lrt | grep $SFTP_RFILE | wc -l
get $SFTP_RFILE
bye 
EOF
 
config.ini
export SFTP_USER = user_name
export SFTP_SERVER = remote_server
export SFTP_RDIR = /home/dir/
export SFTP_RFILE = sample.txt
 
  1. Can I use exported varibles in sftp commands.
  2. Is there a way to check if file exists before get ing it.

Thanks in Advance

  1. yes.
  2. Not like what you would want. Try the ls command that sftp offers

sftp $SFTP_USER@$SERVER <<EOF | grep -q 'No such file'
cd $SFTP_RDIR
ls $SFTP_RFILE
bye 
EOF
if [ $? -eq 1 ] ; then
  echo 'no such remote file'
else
sftp $SFTP_USER@$SERVER <<EOF 
cd $SFTP_RDIR
get $SFTP_RFILE
bye 
EOF
 
fi 

Thanks a Lot Jim. That works.

I have another question:
On Server A, I generated the Public Private keys.
and shared the Public Key in /authorised_keys file on Server B.

Now can I do the passwordless communication from Server B(having public key only) to Server A(having Public as well as Private key)
as well....Or it is only possible from Server A to Server B

---------- Post updated 03-23-12 at 04:59 AM ---------- Previous update was 03-22-12 at 06:07 AM ----------

Please anyone.