In my ssh_config file there are multiple entries for sites on the same server such as:
Host site1
HostName 123.1.1.1
User myuser
Port 13245
GSSAPIAuthentication no
IdentityFile /home/myuser/.ssh/id_dsa
Host site2...
I use a passphrase protected key in order to log in to the remote server and this works fine. However, I'm attempting to create some bash scripts that synchronize files using rsync and would like for the script to to prompt me for the passphrase and then execute the rsync command. ssh-agent seems to be what I want to use but I'm having difficulty figuring it out. I'm looking for something like...
HOST="site1:"
SRC=/var/foo
DEST=/home/bar
SYNC=(rsync "$SRC" "$HOST""$DEST"...)# rsync /var/foo site1:/home/bar...
read -r -p "Are you sure? [y/N] " response
response=${response,,}
if [[ $response =~ ^(yes|y)$ ]]
then
#check to see if the passphrase exists from prior execution of this script.
if [ no ];then
#use ssh-add to prompt for passphrase
ssh-add #? ;
#then execute rsync command
"${SYNC[@]}"
else
#execute rsync command
"${SYNC[@]}"
fi
else
echo "Operation aborted!"
fi
The only examples I've been able to find either suggest code be placed in .bashrc or .profile which forces me to enter a passphrase each time I start a shell or create an expect file which stores the passphrase, neither of which I desire to do. How can I achieve a prompt for the passphrase only once when I start my rsync script so that I can switch hosts and rerun the script as in my example.
if [ no ];then
...?