As Corona688 mentioned using public/private key pairs will be the most secure, Can you give details of why it didn't work(i.e. errors) after you setup the key pairs on each host?
Example use:
sftp -b /dev/stdin -v -o BatchMode=yes -o IdentityFile=/export/home/user/.ssh/id_rsa -o Port=22 user@host <<ENDSFTP
cd /home/user/test
put /export/home/user/test/test.txt test.txt.xferring
rename test.txt.xferring test.txt
quit
ENDSFTP
rc=$?
if [[ $rc != 0 ]] then
print "Error occured...$rc" `date "+%Y-%m-%d-%H.%M.%S"`
else
print "Successful transfer...$rc" `date "+%Y-%m-%d-%H.%M.%S"`
fi