3

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.

3

First you may check whether ssh-agent is running and start it if not:

if ! [ -n "$SSH_AUTH_SOCK" ] || 
  ! { ssh-add -l &>/dev/null; rc=$?; [ "$rc" -eq 0 ] || [ "$rc" -eq 1 ];}; then
    echo "Starting agent..."
    eval "$(ssh-agent -s)"
fi

ssh-add -l exits with code 1 if there are no identities and with code 2 if it cannot connect to ssh-agent.

Then you add the passphrase for the key you need.

ssh-add ~/path/to/keyfile
  • In the ssh-add command is there a way to specify using the path for the identity file from the configured hosts in ssh_config? – dimmech Jan 21 '15 at 17:09
  • Because this is a separate question, I have created another thread here after trying to answer it on my own. Thanks @Hauke – dimmech Jan 22 '15 at 17:36
3

The idea of ssh-agent is to have a running service storing all your keys. Therefore you only need to enter your password once and you can even forward your agent to a remote host if you want to log in to a second host from there.

First verify that ssh-agent is running using ssh-add -l, which probably says "The agent has no identities."

Second you add keys to your agent by saying ssh-add or ssh-add path/to/key/file.

Afterwards rsync, like any other command using ssh, will simply use the agent to fetch the key needed for remote connection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.