Last active
February 23, 2019 02:09
-
-
Save devster31/47c1ed29100d61d443b4c1de38f427b8 to your computer and use it in GitHub Desktop.
ssh-agent automation done well
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SSH_ENV="$HOME/.ssh/environment" | |
function start_agent { | |
echo "Initialising new SSH agent..." | |
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}" | |
echo succeeded | |
chmod 600 "${SSH_ENV}" | |
. "${SSH_ENV}" > /dev/null | |
/usr/bin/ssh-add; | |
} | |
# Source SSH settings, if applicable | |
if [ -f "${SSH_ENV}" ]; then | |
. "${SSH_ENV}" > /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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# setup ssh-agent | |
# | |
# set environment variables if user's agent already exists | |
[ -z "$SSH_AUTH_SOCK" ] && SSH_AUTH_SOCK=$(ls -l /tmp/ssh-*/agent.* 2> /dev/null | grep $(whoami) | awk '{print $9}') | |
[ -z "$SSH_AGENT_PID" -a -z `echo $SSH_AUTH_SOCK | cut -d. -f2` ] && SSH_AGENT_PID=$((`echo $SSH_AUTH_SOCK | cut -d. -f2` + 1)) | |
[ -n "$SSH_AUTH_SOCK" ] && export SSH_AUTH_SOCK | |
[ -n "$SSH_AGENT_PID" ] && export SSH_AGENT_PID | |
# start agent if necessary | |
if [ -z $SSH_AGENT_PID ] && [ -z $SSH_TTY ]; then # if no agent & not in ssh | |
eval `ssh-agent -s` > /dev/null | |
fi | |
# setup addition of keys when needed | |
if [ -z "$SSH_TTY" ] ; then # if not using ssh | |
ssh-add -l > /dev/null # check for keys | |
if [ $? -ne 0 ] ; then | |
alias ssh='ssh-add -l > /dev/null || ssh-add && unalias ssh ; ssh' | |
if [ -f "/usr/lib/ssh/x11-ssh-askpass" ] ; then | |
SSH_ASKPASS="/usr/lib/ssh/x11-ssh-askpass" ; export SSH_ASKPASS | |
fi | |
fi | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~/.config/fish/conf.d/z-ssh_agent_kill.fish | |
function ssh_agent_kill --on-event fish_exit | |
if [ -n "$SSH_AGENT_PID" ] | |
ssh-add -D | |
ssh-agent -k | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~/.config/fish/functions/ssh_agent_start.fish | |
function ssh_agent_start | |
set -l agents (find $TMPDIR -iname 'agent.*' -user (whoami)) | |
if [ count $agents -eq 0 ] | |
eval (ssh-agent -c) | |
ssh-add $HOME/.ssh/* # bad practice, yet convenient | |
else if [ -z "$SSH_AUTH_SOCK" -o -z "$SSH_AGENT_PID" ] | |
setenv SSH_AUTH_SOCK $agents[1] | |
setenv SSH_AGENT_PID (math 1 + (echo $SSH_AUTH_SOCK | cut -d. -f2)) | |
end | |
end | |
# ~/.config/fish/functions/ssh_agent_start_env.fish | |
function ssh_agent_start_env | |
set -q SSH_ENV | |
if [ $status -ne 0 ] | |
set -gx SSH_ENV "$HOME/.ssh/environment" | |
end | |
if [ ! -s $SSH_ENV ] | |
ssh-agent -c | sed '/^echo/d' > $SSH_ENV | |
chmod 600 $SSH_ENV | |
end | |
. $SSH_ENV | |
end | |
# ~/.config/fish/config.fish | |
set -gx SSH_ENV "$HOME/.ssh/environment" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment