blob: 3db641872823f8d7e12938d51c9d6d834f72872d (
plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#!/bin/bash
#------------
# SSH-AGENT
#------------
function sshagent_start {
local key_path="$HOME/.ssh/id_ed25519"
local lifetime="5d"
# Parse options
while getopts "t:k:" opt; do
case "$opt" in
t) lifetime="$OPTARG" ;;
k) key_path="$OPTARG" ;;
*)
echo "Usage: sagent_start [-t lifetime] [-k key_path]"
return 1
;;
esac
done
# Convert lifetime to seconds
local num=${lifetime//[!0-9]/}
local unit=${lifetime//[0-9]/}
local seconds=0
case "$unit" in
s|"") seconds=$num ;; # default to seconds
m) seconds=$((num * 60)) ;;
h) seconds=$((num * 3600)) ;;
d) seconds=$((num * 86400)) ;;
*)
echo "Invalid time unit. Use s, m, h, or d."
return 1
;;
esac
# Clean previous ssh credentials
(rm -rf /tmp/ssh-* > /dev/null)
SSH_ENV="$HOME/.ssh/environment"
printf '\e[1;36m%s\e[m\n' "Initialising new SSH agent..."
ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
chmod 600 "${SSH_ENV}"
# shellcheck source=/dev/null
source "${SSH_ENV}" > /dev/null
if [[ ! -f "$key_path" ]]; then
printf '\e[1;31m%s\e[m\n' "SSH key not found at $key_path"
return 1
fi
if ssh-add -t "$seconds" "$key_path" >/dev/null 2>&1 ; then
printf '\e[1;36m%s\e[m\n' "SSH key added successfully: $key_path (lifetime: $lifetime = ${seconds}s)"
else
printf '\e[1;31m%s\e[m\n' "Failed to add SSH key"
fi
}
function sshagent_stop {
# clean previous ssh credentials
(rm -rf /tmp/ssh-* > /dev/null)
ssh-agent -k > /dev/null
}
function sshagent_findsockets {
find /tmp -uid "$(id -u)" -type s -name agent.\* 2>/dev/null
}
function sshagent_testsocket {
if [ ! -x "$(command -v ssh-add)" ] ; then
echo "ssh-add is not available; agent testing aborted"
return 1
fi
if [ X"$1" != X ] ; then
export SSH_AUTH_SOCK=$1
fi
if [ X"$SSH_AUTH_SOCK" = X ] ; then
return 2
fi
if [ -S "$SSH_AUTH_SOCK" ] ; then
ssh-add -l > /dev/null
if [ $? = 2 ] ; then
echo "Socket $SSH_AUTH_SOCK is dead! Deleting!"
rm -f "$SSH_AUTH_SOCK"
return 4
else
echo "Found ssh-agent $SSH_AUTH_SOCK"
return 0
fi
else
echo "$SSH_AUTH_SOCK is not a socket!"
return 3
fi
}
function sshagent_reload {
# ssh agent sockets can be attached to a ssh daemon process or an
# ssh-agent process.
AGENTFOUND=0
# Attempt to find and use the ssh-agent in the current environment
if sshagent_testsocket ; then AGENTFOUND=1 ; fi
# If there is no agent in the environment, search /tmp for
# possible agents to reuse before starting a fresh ssh-agent
# process.
if [ $AGENTFOUND = 0 ] ; then
for agentsocket in $(sshagent_findsockets) ; do
if [ $AGENTFOUND != 0 ] ; then break ; fi
if sshagent_testsocket "$agentsocket" ; then AGENTFOUND=1 ; fi
done
fi
# If at this point we still haven't located an agent, it's time to
# start a new one
if [ $AGENTFOUND = 0 ] ; then
eval "$(ssh-agent)"
fi
# Clean up
unset AGENTFOUND
unset agentsocket
# Finally, show what keys are currently in the agent
ssh-add -l
}
if [[ -f "$HOME/.ssh/environment" ]]; then
sshagent_reload > /dev/null 2>&1
fi
# Alias agents
alias sagent_start="sshagent_start"
alias sagent_stop="sshagent_stop"
# Clean up not global functions
unset -f sshagent_findsockets sshagent_testsocket
|