-
Notifications
You must be signed in to change notification settings - Fork 0
/
.agent-ssh
51 lines (44 loc) · 1.4 KB
/
.agent-ssh
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
# This script is intended to be sourced from .bashrc
#
# It will start a new ssh-agent when you log in.
# If you forward an agent, than that will be used.
# If the one you started last time has an identity, nothing happens.
#
# It will do nothing to existing apents and only try to interact
# with the one identified by variables in .ssh/vars.sh this script
#
# original script: http://www.nyetwork.org/wiki/SSH
# latest version:
# https://raw.githubusercontent.com/tox2ik/agent-ssh/master/.agent-ssh
#
# --jaroslav, genja.org
#
#
ASK4PASS=${ASK4PASS:-yes}
# Time format examples:
# 600 600 seconds
# 10m 10 minutes
# 1h30m 1 hour 30 minutes
SSH_AGENT_LIFE="-t 7d"
SSH_VARS=$HOME/.ssh/vars.sh
ssh-add -l &>/dev/null; # list identities from forwarded agent
ADD=$?
not_running=1
forwarded_empty=2
if ( [ $ADD -eq $forwarded_empty ] || [ $ADD -eq $not_running ] ) &&
[ -s $SSH_VARS ];
then # there is no agent, but we have a file with ssh-add output
echo "source $SSH_VARS &>/dev/null"
source $SSH_VARS &>/dev/null
fi
if ! ssh-add -l &>/dev/null;
then # nothing forwarded, nothing started/added; run a new agent.
ssh-agent > $SSH_VARS 2>/dev/null
source $SSH_VARS &>/dev/null
fi
if [ $ASK4PASS ]; then
case "$-" in # ask for pass-phrase on interactive shells
*i*) [ "The agent has no identities." = "`ssh-add -l `" ] && ssh-add $SSH_AGENT_LIFE ;;
*) ## stay quiet on non-interactive shells
esac
fi