Skip to content

Instantly share code, notes, and snippets.

@oplatek
Created July 20, 2015 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oplatek/4b0b0aa2c611ae390473 to your computer and use it in GitHub Desktop.
Save oplatek/4b0b0aa2c611ae390473 to your computer and use it in GitHub Desktop.
Ssh agent starting on (multiple machines)
containsElement () {
local e
for e in ${@:2}; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
allowed_ssh_agents=('kronos kronos-dev shrek lrc lrc1 lrc2')
agent_file_prefix="$HOME/.ssh/environment"
hostname=`hostname -s`
agent_file="${agent_file_prefix}${hostname}"
start_agent () {
echo "Initialising new SSH agent for $hostname"
ssh-agent | sed 's/^echo/#echo/' > "$agent_file"
chmod 600 "$agent_file"
. "$agent_file" > /dev/null
alias ssh="ssh -XYC" # Force X-forwarding (+alias adding the key to agent)
ssh-add -l >/dev/null || { alias ssh='ssh-add -l >/dev/null || ssh-add && alias ssh="ssh -XYC"; ssh -XYC'; }
ssh_pid=$(cat $agent_file 2> /dev/null | grep 'AGENT_PID' | sed 's/.*=\([0-9]*\);.*/\1/')
echo "SSH agent(pid: $ssh_pid) started on $hostname"
}
# Source SSH settings, if applicable
# if ( tty -s) ; then
if containsElement "$hostname" "${allowed_ssh_agents[@]}" ; then
echo test allowed
if [ -f "${agent_file}" ] ; then
. "${agent_file}" > /dev/null
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -ef | grep ${SSH_AGENT_PID} | grep 'ssh-agent$' > /dev/null || {
start_agent;
}
else
start_agent;
fi
else
echo "SSH agent on $hostname is not allowed."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment