alex-environ/home/scripts/ssh_agent_setup

79 lines
2.4 KiB
Text
Raw Permalink Normal View History

#!/bin/sh
SSH_ENV=$HOME/.ssh/environment.`hostname`
start_agent ()
{
2022-04-20 14:58:18 +00:00
# $1 Socket path to bind to.
# If not running interactively, don't do anything else
[[ $- != *i* ]] && return
if [ `uname -o` = "Cygwin" ]
then
echo "Connecting to ssh-pageant..."
eval $(/usr/bin/ssh-pageant -r -a "/tmp/.ssh-pageant-$USERNAME")
else
2022-04-20 14:58:18 +00:00
ssh-agent $alex_agent_args > ${SSH_ENV}
chmod 600 ${SSH_ENV}
. ${SSH_ENV} > /dev/null
echo "Created a new ssh-agent $SSH_AGENT_PID"
fi
}
load_keys ()
{
if [ "$alex_load_keys" == "1" ]
then
ssh-add
fi
}
2023-10-03 16:03:01 +00:00
# If not running interactively, don't do anything else
[[ $- != *i* ]] && return
# If we are a subshell we should not do anything.
[[ $0 != -* ]] && return
# Source SSH settings, if applicable
# If a ssh auth socket exists at this point, it must be from a forwarded agent.
2022-03-21 17:09:36 +00:00
if [ -n "${SSH_AUTH_SOCK+set}" ] && [[ ${SSH_AUTH_SOCK} != *"com.apple.launchd"* ]]
then
if [ -f "${SSH_ENV}" ]
then
AUTH_SOCK=$SSH_AUTH_SOCK
. ${SSH_ENV} > /dev/null
kill -9 $SSH_AGENT_PID
pkill -9 -u $USER ssh-agent
rm $SSH_ENV
unset SSH_AGENT_PID
unset SSH_ENV
SSH_AUTH_SOCK=$AUTH_SOCK
export SSH_AUTH_SOCK
fi
echo "Connected to forwarded SSH agent ${SSH_AUTH_SOCK}."
echo "The following keys are available:"
ssh-add -l
else
if [ -f "${SSH_ENV}" ]
then
. ${SSH_ENV} > /dev/null
ssh-add -L > /dev/null 2>&1
case $? in
0) echo "Connected to existing ssh-agent $SSH_AGENT_PID."
echo "The following keys are available:"
ssh-add -l
;;
1) echo "Connected to existing ssh-agent $SSH_AGENT_PID. No keys were found in the agent."
load_keys
;;
2) start_agent
load_keys
;;
esac
else
start_agent
load_keys
fi
fi