Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@haarts

This comment has been minimized.

Copy link

haarts commented May 22, 2013

Oddly enough this works poorly with my fish version 2.0.0. When I run it I get:
fish: Unknown command 'test_identities'

Subsequent runs fail to find the function. Possibly because fish unloads the function?

@haarts

This comment has been minimized.

Copy link

haarts commented May 22, 2013

This does not seem the Way: "It is very important that function definition files only contain the definition for the specified function and nothing else. Otherwise, it is possible that autoloading a function files requires that the function already be loaded, which creates a circular dependency." From http://fishshell.com/docs/2.0/index.html

@gretel

This comment has been minimized.

Copy link

gretel commented Sep 28, 2014

move the function defintions before their calls. guess it never worked that way.

@haakobja

This comment has been minimized.

Copy link

haakobja commented Oct 20, 2016

ps -ef | grep $SSH_AGENT_PID | grep ssh-agent > /dev/null won't work when SSH_AGENT_PID is not set. Instead, it should be
ps -ef | grep ssh-agent > /dev/null

@chksome

This comment has been minimized.

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

This comment has been minimized.

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
You can’t perform that action at this time.