-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetup_ssh.linux.sh
More file actions
33 lines (29 loc) · 849 Bytes
/
setup_ssh.linux.sh
File metadata and controls
33 lines (29 loc) · 849 Bytes
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
# Bash function to add in .bashrc or .profile to idempotently setup an SSH agent for the current user
function setup_ssh {
# set -x
cagent=$( ps ux | awk '/ssh[-]agent/ {print $2}' );
if [ -z ${cagent} ];
then
# Start a new ssh-agent with 24h expiration of keys
eval $( /usr/bin/ssh-agent -t 86400 -s );
else
# Connect to the most recent ssh-agent
SSH_AUTH_SOCK=$(
find /tmp -maxdepth 2 -iname 'agent*' -uid $( id -u ) -print0 2>/dev/null | \
xargs -0 stat -c "%Y %n" | \
sort -n | \
awk '{print $2; exit}'
)
export SSH_AGENT_PID=${cagent};
echo "Agent PID: ${SSH_AGENT_PID}";
fi
# Check if a key is already listed
if ssh-add -l > /dev/null;
then
echo "Identity present in agent";
else
# Add default key to agent
/usr/bin/ssh-add -t 43200;
fi
echo "Done"
}