When I'm running Gnome, ssh-agent will run ssh-add upon slogin/ssh if the identity is not loaded. I want to mimic this behavior on remote shells, but everything I find suggests to run ssh-add upon login. I only want to run ssh-add if I subsequently run slogin/ssh on the remote shell, not every time I login.
I worked out a solution for this via the bash shell.
Add to .bashrc:
There's actually an even simpler way.
ssh-add -l
returns true (0) if there are keys and false (1) otherwise, so your first function can be replaced with this:Even simpler in the «this is an interactive session» part add
Newer versions of OpenSSH can add the key to the agent on first use, if you enable
AddKeysToAgent yes
in your~/.ssh/config
.(I've learned this from https://superuser.com/a/1114257/42546. I'm not sure what the proper StackOverflow etiquette is for coping answers across various StackExchange sites.)