Supply passphrase for ssh in script

I would like to write a bash shell script which will connect to remote server using passphrase. (I have public-private infrastructure created, and as per instruction, I must not use password less ssh).
This particular script will be fired from cron.

Can you please advice how I can supply the passphrase (from any file or any other way) in the script ?

If that "instruction" is based on "security considerations", please be aware that supplying authentication from within a script or even a file is way less secure than e.g. ssh 's public key method!

Thank you so much RudiC for the advice. Yes we will try to make it secure. I tried ssh-add, sshpass, keychain.... but nothing is working.

Please advice.

---------- Post updated at 06:50 AM ---------- Previous update was at 05:55 AM ----------

Got the solution.

  1. Store the password in file or environment variable.
export MYPASS=XXXXX
  1. Export DISPLAY environment variable with some value
export DISPLAY=1
  1. Create a helper file
#!/bin/bash
exec cat
  1. Execute ssh-add
echo $MYPASS|SSH_ASKPASS=./helperfile.sh ssh-add ~/.ssh/id_rsa
1 Like