Skip to content

Commit 62497dc

Browse files
committed
README: add zsh usage
darwin: load into agent keys stored in keychain
1 parent 9ce0dfb commit 62497dc

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

Changes.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
# VERSION - CHANGES
33

4+
### 1.06 - Nov 27, 2019
5+
6+
* SC2086, quote strings
7+
* Update README with zsh instructions
8+
49
### 1.05 - Nov 12, 2013
510

611
* use TMPDIR if defined, default to /tmp (was hard coded /tmp)
@@ -16,10 +21,9 @@
1621

1722
### 1.02 - Dec 16, 2007
1823

19-
* adjusted ps invocation for reliable detection when multiple
20-
* users run ssh-agent on a single system
24+
* adjusted ps invocation for reliable detection when multiple users run ssh-agent on a single system
2125

22-
### 1.01 - Oct 9, 2007
26+
### 1.01 - Oct 9, 2007
2327

2428
* when cleaning up stale agent, remove stale sock file
2529

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ssh-agent
22

3-
A bash script that autoloads the ssh-agent and keys into each terminal session
3+
A shell script that autoloads the ssh-agent and keys into each terminal session
44
of a workstation, significantly reducing the complexity of using ssh-agent.
55

66
Used and tested on Mac OS X, FreeBSD, and Linux computers. Should work on any
@@ -10,12 +10,20 @@ UNIXy host with OpenSSH installed.
1010

1111
1. Install to $HOME/.ssh directory as agent.sh
1212

13-
curl -L -o .ssh/agent.sh https://github.com/msimerson/ssh-agent/raw/master/agent.sh
14-
chmod 755 .ssh/agent.sh
13+
```sh
14+
curl -L -o ~/.ssh/agent.sh https://github.com/msimerson/ssh-agent/raw/master/agent.sh
15+
chmod 755 ~/.ssh/agent.sh
16+
```
1517

1618
2. Run it when new terminal windows open
1719

18-
echo 'source .ssh/agent.sh' >> ~/.bash\_profile
20+
### bash
21+
22+
`echo 'source .ssh/agent.sh' >> ~/.bash\_profile`
23+
24+
### zsh
25+
26+
`echo '.ssh/agent.sh' >> ~/.zprofile`
1927

2028
3. Open new terminal/shell sessions
2129

agent.sh

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,19 @@ TMPDIR=${1-/tmp}
2525
# See README file, SSH-AGENT SOCKET note
2626
_sockfile="${HOME}/.ssh/agent.sock"
2727

28-
if [ "$0" != "-bash" ] && [ "$0" != "-zsh" ];
28+
is_login_shell_bash()
29+
{
30+
expr "$SHELL" : '.*bash' >> /dev/null
31+
if [ $? -eq 0 ]; then
32+
# echo "yes"
33+
return 0
34+
else
35+
# echo "no"
36+
return 1
37+
fi
38+
}
39+
40+
if is_login_shell_bash && [ "$0" != "-bash" ];
2941
then
3042
echo '
3143
OOPS! Did you mean to source this script? Try this:
@@ -34,9 +46,9 @@ OOPS! Did you mean to source this script? Try this:
3446
'
3547
fi
3648

37-
if [ ! -z "$_sockfile" ];
49+
if [ -n "$_sockfile" ];
3850
then
39-
_agent_opts="-a $_sockfile $_agent_opts"
51+
_agent_opts="-s -a $_sockfile $_agent_opts"
4052
fi
4153

4254
main()
@@ -66,6 +78,7 @@ main()
6678
then
6779
echo "ssh-agent socket is stale"
6880
kill "${_agent_pid}" # kill ssh-agent
81+
killall pgrep ssh-agent -U "$USER"
6982
cleanup_stale_agent
7083
start_ssh_agent
7184
return
@@ -74,18 +87,16 @@ main()
7487

7588
set_agent_pid()
7689
{
77-
#echo "checking for ssh-agent process"
78-
_agent_pid=$(pgrep -U "$USER" ssh-agent)
79-
# this is expensive but reliable
80-
#_agent_pid=$(ps uwwxU "$USER" | grep ssh-agent | grep -v grep | awk '{ print $2 }' | tail -n1)
90+
# echo "checking for ssh-agent process"
91+
_agent_pid=$(pgrep -U "$USER" ssh-agent | tail -n1)
8192
}
8293

8394
set_socket_file()
8495
{
8596
# if _sockfile is not defined we must figure it out
8697
if [ -z "$_sockfile" ] || [ ! -e "$_sockfile" ];
8798
then
88-
_sock_pid=$(echo "${_agent_pid} - 1" | bc)
99+
_sock_pid=$(echo "${_agent_pid}" - 1 | bc)
89100
_sockfile=$(/bin/ls "$TMPDIR/ssh-*/agent.${_sock_pid}")
90101
fi
91102

@@ -122,14 +133,19 @@ discover_ssh_agent()
122133
start_ssh_agent()
123134
{
124135
echo "starting ssh-agent $_agent_opts"
125-
ssh-agent $_agent_opts > /dev/null
136+
# shellcheck disable=SC2046,SC2086
137+
eval $(ssh-agent $_agent_opts) || return
126138

127139
discover_ssh_agent
128140

129-
if [ ! -z "$SSH_AUTH_SOCK" ];
141+
if [ -n "$SSH_AUTH_SOCK" ];
130142
then
131-
# this will prompt the user to authenticate their ssh key(s)
132143
echo "adding ssh key(s) to agent"
144+
if [ "$(uname)" = "Darwin" ]; then
145+
# loads SSH keys stored in OS X Keychain
146+
ssh-add -A
147+
fi
148+
# this will prompt the user to authenticate their password protected ssh key(s)
133149
ssh-add
134150
fi
135151
}

0 commit comments

Comments
 (0)