-
Notifications
You must be signed in to change notification settings - Fork 14
/
start-ssh-agent
executable file
·41 lines (35 loc) · 1.34 KB
/
start-ssh-agent
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
#!/usr/bin/env bash
#
# Start ssh-agent if it is not running (by becoming it)
# Intended for use on Bash for Windows using the WSL
#
# Author: Dave Eddy <dave@daveeddy.com>
# Date: October 09, 2017
# License: MIT
# Could be any file - nothing intrinsically valuable about ~/.ssh/environment
envfile=~/.ssh/environment
# Ensure the environment file exists and has its permissions properly set.
# Source the file - if it was created by this script the source will
# effectively be a noop.
mkdir -p "${envfile%/*}"
touch "$envfile"
chmod 600 "$envfile"
. "$envfile"
# Check if the daemon is already running
if [[ -n $SSH_AGENT_PID ]] && kill -0 "$SSH_AGENT_PID" 2>/dev/null; then
# The PID is up but it could have been recycled - attempt to list keys.
# This will exit with 2 if the SSH_AUTH_SOCK is broken.
ssh-add -l &>/dev/null
if (($? != 2)); then
echo "alreading running: $SSH_AGENT_PID"
exit 1
fi
fi
# Overwrite what is in the envfile to start a fresh ssh-agent instance
echo "# Started $(date)" > "$envfile"
# For some reason, this line doesn't get emitted by ssh-agent when it is run
# with -d or -D. Since we are starting the program with exec we already know
# the pid ahead of time though so we can create this line manually
echo "SSH_AGENT_PID=$$; export SSH_AGENT_PID" >> "$envfile"
# Become ssh-agent and run forever
exec ssh-agent -D >> "$envfile"