Skip to content

Instantly share code, notes, and snippets.

@psyllo
Created December 13, 2013 05:36
Show Gist options
  • Save psyllo/7940194 to your computer and use it in GitHub Desktop.
Save psyllo/7940194 to your computer and use it in GitHub Desktop.
ssh-agent-start.sh - handy for starting an ssh-agent
#!/usr/bin/env bash
#
# Author: Benjamin Cluff - 2013
#
# Synopsis:
#
# Start ssh-agent and create file containing the environment
# variables that can be sourced by .bash_profile so that other ssh
# commands (ssh, ssh-add) can find it.
#
# A new bash shell is started with the ssh-agent env vars, but it is
# not necessary if the file with the env vars is sourced.
#
# Messages are piped to stderr so stout is safe to evaluate.
#
ssh_agent_env_file=~/.ssh-agent-env
if [ -r $ssh_agent_env_file ]; then
source $ssh_agent_env_file >&2
fi
if [ -n "$SSH_AGENT_PID" ]; then
echo "Another ssh-agent may be running with PID: $SSH_AGENT_PID" >&2
echo "Attempting to 'ssh-agent kill $SSH_AGENT_PID' first." >&2
ssh-agent kill $SSH_AGENT_PID >&2
sleep 2
fi
ssh-agent > $ssh_agent_env_file
if [ $? == 0 ]; then
echo "Example for adding your key to agent: ssh-add ~/.ssh/id_rsa" >&2
echo 'Starting new shell with updated env vars...' >&2
source $ssh_agent_env_file > /dev/null 2>&1
bash >&2
echo
echo "Leaving bash shell created by '$0'" >&2
echo 'You can evaulate the following to set the env vars in this shell:' >&2
echo
cat $ssh_agent_env_file
echo
else
echo 'There was a problem starting the ssh-agent.' >&2
exit 1
fi
@hbeni
Copy link

hbeni commented Nov 28, 2023

In 2008, I have come up with this (funnily same named) solution; it also does automatically add your keys.
Its to be dropped in your personal ~/bin folder and then called in ~/.bashrclike this:

# If running interactively, then:
if [ "$PS1" ]; then
    . ~/bin/ssh_agent_start.sh
...

~/bin/ssh_agent_start.sh contains:

#!/bin/bash
#
# Script überprüft ob ein SSH_AGENT für den User läuft.
# Falls nicht wird ein neuer gestartet und alle gefundenen
# Keys hinzugefügt.
#
# @author  Benedikt Halinger <[email protected]>
# @version 2008-05-29 13:27
#

# Hier speichert der Agent seine Konfiguration
AGENTFILE=/tmp/.${USER}_ssh-agent

if [[ -f $AGENTFILE ]]; then
        source $AGENTFILE &> /dev/null
fi

# Alle gefundenen IDs hinzufügen
# (IDs sind files unter ~/.ssh, welche id_ heissen und keine .pub erweiterung haben.
# Alles IDs die dieser Regel nicht folgen, müssen manuell mit `ssh-add` hinzugefügt werden!)
function import_keys {
        find ~/.ssh -name "id_*" ! -name "*.*" -exec ssh-add "{}" \;
}


if [[ "$SSH_AGENT_PID" != "" && `pgrep -n -u $USER ssh-agent` == $SSH_AGENT_PID ]];
then
        # Agentkonfig gefunden. Die dort angegebene PID ist vorhanden und ein
        # ssh-agent Prozess des Users. Agent einbinden.
        echo "ssh-agent ($SSH_AGENT_PID) found, exporting settings..."

        KEYS=`ssh-add -l`;
        if [[ "$KEYS" =~ "no identities" ]]; then
                echo $KEYS
                import_keys
        fi
else
        # Kein Agent am laufen oder kein Agentfile. Neuen Agent starten.

        # kille alte agentprozesse des users, da nicht mehr zugänglich
        for OLDPIDS in `pgrep -u $USER ssh-agent`; do
                echo "killing unavailable agent ($OLDPIDS)"
                kill $OLDPIDS
        done

        touch $AGENTFILE; chmod go-rwx $AGENTFILE
        /usr/bin/ssh-agent > $AGENTFILE
        source $AGENTFILE &> /dev/null
        echo "started new ssh-agent ($SSH_AGENT_PID)"

        import_keys
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment