forked from wwalker/ssh-find-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-find-agent.bash
113 lines (95 loc) · 2.44 KB
/
ssh-find-agent.bash
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
104
105
106
107
108
109
110
111
112
113
# Copyright (C) 2011 by Wayne Walker <[email protected]>
#
# Released under one of the versions of the MIT License.
#
# See LICENSE for details
_LIVE_AGENT_LIST=""
_debug_print() {
if [[ $_DEBUG -gt 0 ]]
then
printf "%s\n" $1
fi
}
find_all_ssh_agent_sockets() {
_SSH_AGENT_SOCKETS=`find /tmp/ -type s -name agent.\* 2> /dev/null | grep '/tmp/ssh-.*/agent.*'`
_debug_print "$_SSH_AGENT_SOCKETS"
}
find_all_gpg_agent_sockets() {
_GPG_AGENT_SOCKETS=`find /tmp/ -type s -name S.gpg-agent.ssh 2> /dev/null | grep '/tmp/gpg-.*/S.gpg-agent.ssh'`
_debug_print "$_GPG_AGENT_SOCKETS"
}
find_all_gnome_keyring_agent_sockets() {
_GNOME_KEYRING_AGENT_SOCKETS=`find /tmp/ -type s -name ssh 2> /dev/null | grep '/tmp/keyring-.*/ssh$'`
_debug_print "$_GNOME_KEYRING_AGENT_SOCKETS"
}
find_all_osx_keychain_agent_sockets() {
_OSX_KEYCHAIN_AGENT_SOCKETS=`find /tmp/ -type s -regex '.*/launch-.*/Listeners$' 2> /dev/null`
_debug_print "$_OSX_KEYCHAIN_AGENT_SOCKETS"
}
test_agent_socket() {
local SOCKET=$1
SSH_AUTH_SOCK=$SOCKET ssh-add -l 2> /dev/null > /dev/null
result=$?
_debug_print $result
if [[ $result -eq 0 ]]
then
# contactible and has keys loaded
_KEY_COUNT=`SSH_AUTH_SOCK=$SOCKET ssh-add -l | wc -l | tr -d ' '`
fi
if [[ $result -eq 1 ]]
then
# contactible butno keys loaded
_KEY_COUNT=0
fi
if [[ ( ( $result -eq 0 ) || ( $result -eq 1 ) ) ]]
then
if [[ -n "$_LIVE_AGENT_LIST" ]]
then
_LIVE_AGENT_LIST="${_LIVE_AGENT_LIST} ${SOCKET}:$_KEY_COUNT"
else
_LIVE_AGENT_LIST="${SOCKET}:$_KEY_COUNT"
fi
return 0
fi
return 1
}
find_live_gnome_keyring_agents() {
for i in $_GNOME_KEYRING_AGENT_SOCKETS
do
test_agent_socket $i
done
}
find_live_osx_keychain_agents() {
for i in $_OSX_KEYCHAIN_AGENT_SOCKETS
do
test_agent_socket $i
done
}
find_live_gpg_agents() {
for i in $_GPG_AGENT_SOCKETS
do
test_agent_socket $i
done
}
find_live_ssh_agents() {
for i in $_SSH_AGENT_SOCKETS
do
test_agent_socket $i
done
}
find_all_agent_sockets() {
_LIVE_AGENT_LIST=
find_all_ssh_agent_sockets
find_all_gpg_agent_sockets
find_all_gnome_keyring_agent_sockets
find_all_osx_keychain_agent_sockets
find_live_ssh_agents
find_live_gpg_agents
find_live_gnome_keyring_agents
find_live_osx_keychain_agents
_debug_print "$_LIVE_AGENT_LIST"
printf "%s\n" "$_LIVE_AGENT_LIST" | sed -e 's/ /\n/g' | sort -n -t: -k 2 -k 1
}
set_ssh_agent_socket() {
export SSH_AUTH_SOCK=$(find_all_agent_sockets|tail -n 1|awk -F: '{print $1}')
}