Inoltre, insieme a tutto questo, è possibile trovare un processo ssh-agent esistente e aggiungervi le mie chiavi?
Sì. Possiamo memorizzare le informazioni di connessione in un file:
# Ensure agent is running
ssh-add -l &>/dev/null
if [ "$?" == 2 ]; then
    # Could not open a connection to your authentication agent.
    # Load stored agent connection info.
    test -r ~/.ssh-agent && \
        eval "$(<~/.ssh-agent)" >/dev/null
    ssh-add -l &>/dev/null
    if [ "$?" == 2 ]; then
        # Start agent and store agent connection info.
        (umask 066; ssh-agent > ~/.ssh-agent)
        eval "$(<~/.ssh-agent)" >/dev/null
    fi
fi
# Load identities
ssh-add -l &>/dev/null
if [ "$?" == 1 ]; then
    # The agent has no identities.
    # Time to add one.
    ssh-add -t 4h
fi
Questo codice proviene dalle trappole degli agenti ssh che descrive sia le trappole di ciò che stai facendo attualmente, di questo approccio, sia come dovresti usare ssh-ident per farlo per te.
Se vuoi eseguire ssh-agent solo se non è in esecuzione e non fare altro:
if [ $(ps ax | grep [s]sh-agent | wc -l) -gt 0 ] ; then
    echo "ssh-agent is already running"
else
    eval $(ssh-agent -s)
    if [ "$(ssh-add -l)" == "The agent has no identities." ] ; then
        ssh-add ~/.ssh/id_rsa
    fi
    # Don't leave extra agents around: kill it on exit. You may not want this part.
    trap "ssh-agent -k" exit
fi
 Tuttavia, questo non garantisce ssh-agent sarà accessibile (solo perché è in esecuzione non significa che abbiamo $SSH_AGENT_PID per ssh-add a cui connettersi).
No, davvero, come controllare se ssh-agent è già in esecuzione in bash?
 Le risposte finora non sembrano rispondere alla domanda originale...
Ecco cosa funziona per me:
if ps -p $SSH_AGENT_PID > /dev/null
then
   echo "ssh-agent is already running"
   # Do something knowing the pid exists, i.e. the process with $PID is running
else
eval `ssh-agent -s`
fi
Questo è stato preso da qui
Se vuoi che venga ucciso subito dopo l'uscita dello script, puoi semplicemente aggiungere questo dopo la riga eval:
trap "kill $SSH_AGENT_PID" exit
Oppure:
trap "ssh-agent -k" exit
 $SSH_AGENT_PID viene impostato nella valutazione di ssh-agent -s .
 Dovresti essere in grado di trovare ssh-agent in esecuzione s scansionando /tmp/ssh-* e ricostruire l'SSH_AGENT variabili da esso (SSH_AUTH_SOCK e SSH_AGENT_PID ).
 ps -p $SSH_AGENT_PID > /dev/null || eval "$(ssh-agent -s)" 
Comando a riga singola. Esegui per la prima volta avvierà ssh-agent. Esegui per la seconda volta non avvierà ssh-agent. Compagno semplice ed elegante!!!