Skip to content

Instantly share code, notes, and snippets.

@schaary
Created May 11, 2012 07:33
Show Gist options
  • Save schaary/2658156 to your computer and use it in GitHub Desktop.
Save schaary/2658156 to your computer and use it in GitHub Desktop.
Auto-launching ssh-agent in fish shell
setenv SSH_ENV $HOME/.ssh/environment
if [ -n "$SSH_AGENT_PID" ]
ps -ef | grep $SSH_AGENT_PID | grep ssh-agent > /dev/null
if [ $status -eq 0 ]
test_identities
end
else
if [ -f $SSH_ENV ]
. $SSH_ENV > /dev/null
end
ps -ef | grep $SSH_AGENT_PID | grep -v grep | grep ssh-agent > /dev/null
if [ $status -eq 0 ]
test_identities
else
start_agent
end
end
function start_agent
echo "Initializing new SSH agent ..."
ssh-agent -c | sed 's/^echo/#echo/' > $SSH_ENV
echo "succeeded"
chmod 600 $SSH_ENV
. $SSH_ENV > /dev/null
ssh-add
end
function test_identities
ssh-add -l | grep "The agent has no identities" > /dev/null
if [ $status -eq 0 ]
ssh-add
if [ $status -eq 2 ]
start_agent
end
end
end
@chksome
Copy link

chksome commented Dec 6, 2016

@haakobja Isn't that what if [ -n "$SSH_AGENT_PID" ] is for? It checks to make sure SSH_AGENT_PID is non-zero and set? I'm a fish n00b so I could very well be wrong.

@ivakyb
Copy link

ivakyb commented Mar 5, 2019

You can try fresher implementation https://github.com/ivakyb/fish_ssh_agent

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