Last active
September 20, 2020 18:23
-
-
Save IronSavior/faf979d516b7f14e6d9f340aac14e41d to your computer and use it in GitHub Desktop.
Start SSH agent at login
This file contains hidden or 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
#!/usr/bin/env bash | |
# Write paths to keys to autoload in ~/.autoload-ssh-keys | |
env=~/.ssh/agent.env | |
autoload=~/.autoload-ssh-keys | |
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; } | |
agent_start () { | |
(umask 077; ssh-agent >| "$env") | |
. "$env" >| /dev/null ; } | |
test -f "$autoload" && readarray -t keys <~/.autoload-ssh-keys | |
agent_load_env | |
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running | |
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?) | |
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then | |
agent_start | |
ssh-add "${keys[@]}" | |
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then | |
ssh-add | |
ssh-add "${keys[@]}" | |
fi | |
unset env |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment