-
Notifications
You must be signed in to change notification settings - Fork 0
/
.sshmagic
executable file
·50 lines (43 loc) · 1.55 KB
/
.sshmagic
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
#!/bin/bash
# some ness for making the same instance of ssh-agent is accessible instead of
# having to start a new instance and load keys for each shell. Also set up
# load-keys function.
KERNEL=$(uname)
SSH_ENV="$HOME/.ssh/environment"
[ -d $(dirname $SSH_ENV) ] || mkdir -p $(dirname $SSH_ENV)
function load-keys {
# Exclusion array for file patterns we know are not ssh keys
# Add any additional patterns to this array to exclude.
# For some reason zsh on OSX doesn't like direct initialization
# of the array on OSX
# (though this works totally fine on zsh in Linux)
# The below should work regardless of platform or zsh/bash
local -a EXCLUDE_PATTERNS;EXCLUDE_PATTERNS=('"*.*"'
'"environment"'
'"known_hosts"'
'"config"'
'"noload*"'
'"*~"'
'"#*#"')
echo Loading SSH keys into ssh-agent...
eval "find $HOME/.ssh/ -maxdepth 1 -type f \( $(printf " ! -iname %s" "${EXCLUDE_PATTERNS[@]}") \) -exec ssh-add {} \;"
}
function start_agent {
echo "Initialising new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
#echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
echo "Loading keys with 'load-keys' (Ctrl-C to cancel, load-keys can be manually called later)"
load-keys
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi