SFTP scenario

#!/usr/bin/ksh

Archive_Dir='/apps/SrcFiles/MTCHG_GFTS/BRGR/Archive'
Source_Dir='/apps/SrcFiles/MTCHG_GFTS/BRGR'

cd $Source_Dir

HOST='xyz.abc.com'
USER='abcOUT'
PSW='xyzOUT'
file="Request*.pgp"

for i in 1 2 3 4 5 6
do		
sftp $USER@$HOST <<END_SCRIPT
$PSW
bin 
if [[ -f $file ]] ; then
  #echo "File FOUND!"
	mget *.pgp
	bye
	break
	#else
	#echo "File not found. Will try again in next hour!"
fi
END_SCRIPT
 		sleep 3600
done

What I am trying to do is check for a file using SFTP on a server and if it exists download it to local server. If it does not exist then I need to try again after another hour--but only for a maximum of 6 times. If the file does not exist at the 6th attempt the script should exit.

2 Issues:
1) SFTP without it asking for a password. I run this script and it keeps asking for a password and then gives me some random errors saying invalid command which I think is the "If file exists check". Maybe that does not work in sftp.
2) The SFTP connection has a timeout set to every 15 minutes so I want the script to create a new session everytime it looks for a file. I am not sure if my script is actually doing that.

I know that I can specify a batch file that can be used as input to allow for it to pick up the password automatically but I couldn't figure that out. Any help suggestions--welcome.

Copy your public key from your local machine, ~/.ssh/id_dsa.pub or ~/.ssh/id_rsa.pub, to the remote machine and append it to ~/.ssh/authorized_keys. If that file doesn't exist, just rename your .pub file to that name.

Make sure directory ~/.ssh has file mode 0700 (chmod). The authorized_keys file may also need to be 0600 or similar.

Now you should be able to use sftp, ssh and scp with no password.

I really don't want to mess with the keys. I do not believe I have access to the keys but if nothing else works then I will have to request permission.

Anyhow something like the below works for me. Don't know if I will be able to incorporate if with the other requirements in my script. This expect thing looks cool!

#!/usr/local/bin/expect

spawn  sftp  -b cmdFile user@yourserver.com
expect "password:"
send "shhh!\n";
interact

Take a look at a utility called LFTP, it is a great FTP "shell" which handles multiple flavors of S/FTP and is quite scriptable with repeated attempts and error-handling.