mirror of
https://github.com/robbyrussell/oh-my-zsh.git
synced 2026-09-28 14:06:12 +02:00
fix(ssh-agent): add honor-existing to reuse a running agent (#14049)
This commit is contained in:
@@ -21,6 +21,18 @@ To enable **agent forwarding support** add the following to your zshrc file:
|
|||||||
zstyle :omz:plugins:ssh-agent agent-forwarding yes
|
zstyle :omz:plugins:ssh-agent agent-forwarding yes
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `honor-existing`
|
||||||
|
|
||||||
|
If your session already provides an ssh-agent — a desktop keyring, `gpg-agent`,
|
||||||
|
KeePassXC, or an agent forwarded over SSH — the plugin normally ignores it and
|
||||||
|
starts one of its own, which leaves you with two agents. Set this to reuse the
|
||||||
|
running agent instead, and only start a new one when `$SSH_AUTH_SOCK` is empty
|
||||||
|
or does not answer:
|
||||||
|
|
||||||
|
```zsh
|
||||||
|
zstyle :omz:plugins:ssh-agent honor-existing yes
|
||||||
|
```
|
||||||
|
|
||||||
### `helper`
|
### `helper`
|
||||||
|
|
||||||
To set an **external helper** to ask for the passwords and possibly store
|
To set an **external helper** to ask for the passwords and possibly store
|
||||||
|
|||||||
@@ -1,16 +1,28 @@
|
|||||||
# Get the filename to store/lookup the environment from
|
# Get the filename to store/lookup the environment from
|
||||||
ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
|
ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
|
||||||
|
|
||||||
|
# Test if $SSH_AUTH_SOCK points at a listening agent
|
||||||
|
function _is_agent_running() {
|
||||||
|
local REPLY
|
||||||
|
[[ -n "$SSH_AUTH_SOCK" && -S "$SSH_AUTH_SOCK" ]] || return 1
|
||||||
|
zmodload zsh/net/socket 2>/dev/null || return 1
|
||||||
|
zsocket "$SSH_AUTH_SOCK" 2>/dev/null || return 1
|
||||||
|
exec {REPLY}>&- # close the probe connection
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
function _start_agent() {
|
function _start_agent() {
|
||||||
|
# Reuse an agent the session already provides, if enabled
|
||||||
|
if zstyle -t :omz:plugins:ssh-agent honor-existing && _is_agent_running; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
# Check if ssh-agent is already running
|
# Check if ssh-agent is already running
|
||||||
if [[ -f "$ssh_env_cache" ]]; then
|
if [[ -f "$ssh_env_cache" ]]; then
|
||||||
. "$ssh_env_cache" > /dev/null
|
. "$ssh_env_cache" > /dev/null
|
||||||
|
|
||||||
# Test if $SSH_AUTH_SOCK is visible
|
# Test if $SSH_AUTH_SOCK is visible
|
||||||
zmodload zsh/net/socket
|
_is_agent_running && return 0
|
||||||
if [[ -S "$SSH_AUTH_SOCK" ]] && zsocket "$SSH_AUTH_SOCK" 2>/dev/null; then
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ! -d "$HOME/.ssh" ]]; then
|
if [[ ! -d "$HOME/.ssh" ]]; then
|
||||||
@@ -124,4 +136,4 @@ if ! zstyle -t :omz:plugins:ssh-agent lazy; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
unset agent_forwarding ssh_env_cache
|
unset agent_forwarding ssh_env_cache
|
||||||
unfunction _start_agent _add_identities
|
unfunction _start_agent _add_identities _is_agent_running
|
||||||
|
|||||||
Reference in New Issue
Block a user