-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_ssh
58 lines (46 loc) · 1.09 KB
/
.bash_ssh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Save ssh key
source ~/.bash_functions
if fails type log_debug; then
function log_debug() {
: # no-op
}
fi
# Note: ~/.ssh/environment should not be used, as it
# already has a different purpose in SSH
env=~/.ssh/agent.env
# Note: Don't bother checking SSH_AGENT_PID. It's not used
# by SSH itself, and it might even be incorrect
# (for example, when using agent-forwarding over SSH).
agent_is_running() {
if [ "$SSH_AUTH_SOCK" ]; then
# ssh-add returns:
# 0 = agent running, has keys
# 1 = agent running, no keys
# 2 = agent not running
ssh-add -l >&/dev/null || [ $? -eq 1 ]
else
false
fi
}
agent_has_keys() {
ssh-add -l >/dev/null 2>&1
}
agent_load_env() {
source ${env} >/dev/null
}
agent_start() {
(umask 077; ssh-agent > ${env})
source ${env} >/dev/null
}
if ! agent_is_running; then
log_debug "Loading SSH environment."
agent_load_env
fi
if ! agent_is_running; then
log_debug "Starting ssh-agent"
agent_start
ssh-add
elif ! agent_has_keys; then
ssh-add
fi
unset env