-
Notifications
You must be signed in to change notification settings - Fork 89
/
ssh.bashrc
executable file
·103 lines (94 loc) · 2.81 KB
/
ssh.bashrc
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# See also: https://github.com/wwalker/ssh-find-agent
#
## SSH
SSH_ENV=${SSH_ENV:-"$HOME/.ssh/environment"}
SSH_KEYS="${SSH_KEYS:-}"
## Stop on logout
# See http://jowisoftware.de/wp/2012/04/managing-ssh-agent-automatically-with-cygwinputty-support/
if [[ "$OSTYPE" = cygwin ]] ; then
__instances_tgt=1
_count_instances () {
ps | grep -wc [m]intty
}
else
__instances_tgt=2
function _count_instances() {
# ps aux | grep -wc [b]ash
# The fact this is called in a function adds one to number bash
# processes detected -> __instances_tgt=2
pgrep -u $USER bash | wc -l
}
fi
_on_exit_stop_agent() {
local nb_instances=$(_count_instances)
if [ ${nb_instances} -eq ${__instances_tgt} ] ; then
echo "Terminating ssh-agent"
ssh-add -D >/dev/null 2>&1
ssh-agent -k >/dev/null 2>&1
else
echo "$((${nb_instances}-1)) terminals still running. ssh-agent is kept."
fi
}
trap '_on_exit_stop_agent' EXIT
# start the ssh-agent
function start_agent {
echo "Initializing new SSH agent..."
# spawn ssh-agent
test -f "${SSH_ENV}" && rm "${SSH_ENV}"
ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
ssh-add ${SSH_KEYS}
}
# test for identities
function test_identities {
# test whether standard identities have been added to the agent already
ssh-add -l | grep "The agent has no identities" > /dev/null
if [ $? -eq 0 ]; then
ssh-add ${SSH_KEYS}
# $SSH_AUTH_SOCK broken so we start a new proper agent
if [ $? -eq 2 ];then
start_agent
fi
fi
}
if [ ${SSH_AGENT_IS_HIDDEN:-0} -eq 1 ] ; then
# Sometimes, I'm on a machine that hides ssh-agent process.
# Let's instead suppose that if ssh-add -l returns a valid number
# which means there is an agent running around
function _ssh_test_agent {
ssh-add -l > /dev/null 2>&1
}
else
function _ssh_test_agent {
ps -ef | grep $SSH_AGENT_PID | grep ssh-agent > /dev/null
}
fi
function _ssh_run_env {
# if $SSH_AGENT_PID is not properly set, we might be able to load one from
# $SSH_ENV
. "${SSH_ENV}" > /dev/null
_ssh_test_agent && test_identities || start_agent
}
# check for running ssh-agent with proper $SSH_AGENT_PID
[ -n "$SSH_AGENT_PID" ] && _ssh_test_agent \
&& test_identities \
|| _ssh_run_env
return
# This (old) version generates the following error message:
# Usage : grep [OPTION]... PATTERN [FILE]...
ps -ef | grep $SSH_AGENT_PID | grep ssh-agent > /dev/null
if [ $? -eq 0 ]; then
test_identities
# if $SSH_AGENT_PID is not properly set, we might be able to load one from
# $SSH_ENV
else
. "${SSH_ENV}" > /dev/null
ps -ef | grep $SSH_AGENT_PID | grep ssh-agent > /dev/null
if [ $? -eq 0 ]; then
test_identities
else
start_agent
fi
fi