-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssh-agent-env.rc
More file actions
94 lines (78 loc) · 2.09 KB
/
Copy pathssh-agent-env.rc
File metadata and controls
94 lines (78 loc) · 2.09 KB
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
# load_agent_env will either create a new agent for a named key
# or recover the existing agent if it exists
# the agent's details live in ~/.ssh/.ssh-agent-$keyname
# shellcheck shell=bash
new_agent() {
if [ -z "$1" ] ; then
echo "Usage: new_agent keyname"
return
fi
local KEY=$1
local FILE=${HOME}/.ssh/.ssh-agent-${KEY}
# shellcheck disable=1090
"${HOME}/bin/create-ssh-agent-for-key" "${KEY}" && . "${FILE}"
}
load_agent_env() {
if [ -z "$1" ] ; then
echo "Usage: load_agent_env keyname"
return
fi
local KEY=$1
test_agent_env "${KEY}"
local _what=$?
if [ ${_what} -eq 1 ] ; then
# no agent - create, load keys
new_agent "$1"
"${HOME}/bin/load-key" "${KEY}"
elif [ ${_what} -eq 2 ] ; then
# agent exists but no keys
"${HOME}/bin/load-key" "${KEY}"
fi
}
test_agent_env() {
if [ -z "$1" ] ; then
echo "Usage: test_agent_env keyname"
return
fi
local KEY=$1
local FILE=${HOME}/.ssh/.ssh-agent-${KEY}
if [ -f "${FILE}" ] ; then
echo "found ${FILE} for ${KEY}, loading"
# shellcheck disable=1090
. "${FILE}"
if ! ps -p "${SSH_AGENT_PID}" 2>/dev/null | grep -q ssh-agent ; then
echo "Stale agent details for ${KEY}"
return 1
fi
if ! ssh-add -l ; then
echo "No keys loaded for ${KEY}"
return 2
fi
else
echo "No agent details found for ${KEY}"
return 1
fi
return 0
}
# kill_agent kills the agent for a named key
kill_agent() {
if [ -z "$1" ] ; then
echo "Usage: kill_agent keyname"
return
fi
local KEY=$1
local FILE=${HOME}/.ssh/.ssh-agent-${KEY}
if [ -f "${FILE}" ] ; then
echo "Found ${FILE} for ${KEY}, cleaning up"
# shellcheck disable=1090
. "${FILE}"
kill "${SSH_AGENT_PID}"
rm "${FILE}"
else
echo "No agent details found for ${KEY}"
fi
}
alias la=load_agent_env
alias xsh='ssh-with-lookup'
alias xcp='scp-with-lookup'
# vim: ft=sh