Last active
June 23, 2026 09:22
-
-
Save hostmaster/c1d168b3ccda2e25c3f809674d8502a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # macOS SSH Agent Configuration - Using launchctl approach | |
| # Get system SSH_AUTH_SOCK from launchd | |
| if test -z "$SSH_AUTH_SOCK" | |
| set -gx SSH_AUTH_SOCK (launchctl getenv SSH_AUTH_SOCK) | |
| end | |
| if test -n "$SSH_AUTH_SOCK" | |
| command ssh-add -l &>/dev/null | |
| set -l status_code $status | |
| switch $status_code | |
| case 0 | |
| return 0 | |
| case 1 | |
| if command ssh-add --apple-load-keychain &>/dev/null | |
| return 0 | |
| end | |
| echo "[ssh-agent] Warning: Failed to load keys from keychain" >&2 | |
| return 0 | |
| end | |
| end | |
| # Fallback: Start custom agent if system one is not available | |
| # Use a shared socket so other terminals can reuse it | |
| set -l _agent_env "$HOME/.ssh/agent-env" | |
| if test -f "$_agent_env" | |
| source "$_agent_env" | |
| command ssh-add -l &>/dev/null | |
| if test $status -le 1 | |
| return 0 | |
| end | |
| end | |
| set -l agent_output (command ssh-agent -t 4h) | |
| or begin | |
| echo "[ssh-agent] Failed to start custom SSH agent" >&2 | |
| return 1 | |
| end | |
| for line in (string match -r 'SSH_[A-Z_]+=[^;]+' $agent_output) | |
| set -l kv (string split '=' $line) | |
| set -gx $kv[1] $kv[2] | |
| end | |
| printf "set -gx SSH_AUTH_SOCK %s\nset -gx SSH_AGENT_PID %s\n" "$SSH_AUTH_SOCK" "$SSH_AGENT_PID" >"$_agent_env" | |
| function _ssh_agent_cleanup --on-event fish_exit | |
| kill $SSH_AGENT_PID 2>/dev/null | |
| rm -f "$HOME/.ssh/agent-env" | |
| end | |
| if not command ssh-add --apple-load-keychain &>/dev/null | |
| if not command ssh-add &>/dev/null | |
| echo "[ssh-agent] Warning: Failed to load SSH keys" >&2 | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env zsh | |
| # macOS SSH Agent Configuration - Using launchctl approach | |
| # Get system SSH_AUTH_SOCK from launchd | |
| if [[ -z $SSH_AUTH_SOCK ]]; then | |
| export SSH_AUTH_SOCK=$(launchctl getenv SSH_AUTH_SOCK) | |
| fi | |
| if [[ -n $SSH_AUTH_SOCK ]]; then | |
| # Check agent status via ssh-add -l exit code: | |
| # 0 = agent running, keys loaded | |
| # 1 = agent running, no keys | |
| # 2 = cannot connect to agent | |
| command ssh-add -l &>/dev/null | |
| case $? in | |
| 0) return 0 ;; | |
| 1) | |
| command ssh-add --apple-load-keychain &>/dev/null && return 0 | |
| print -u2 "[ssh-agent] Warning: Failed to load keys from keychain" | |
| return 0 | |
| ;; | |
| esac | |
| fi | |
| # Fallback: Start custom agent if system one is not available | |
| # Use a shared socket so other terminals can reuse it | |
| local _agent_env="$HOME/.ssh/agent-env" | |
| if [[ -f $_agent_env ]]; then | |
| source "$_agent_env" &>/dev/null | |
| # Verify the existing agent is still alive | |
| command ssh-add -l &>/dev/null || [[ $? -eq 1 ]] && return 0 | |
| fi | |
| eval "$(command ssh-agent -t 4h)" || { | |
| print -u2 "[ssh-agent] Failed to start custom SSH agent" | |
| return 1 | |
| } | |
| print "export SSH_AUTH_SOCK=$SSH_AUTH_SOCK\nexport SSH_AGENT_PID=$SSH_AGENT_PID" > "$_agent_env" | |
| _ssh_agent_cleanup() { kill $SSH_AGENT_PID 2>/dev/null; rm -f "$HOME/.ssh/agent-env" } | |
| autoload -Uz add-zsh-hook | |
| add-zsh-hook zshexit _ssh_agent_cleanup | |
| if ! command ssh-add --apple-load-keychain &>/dev/null && ! command ssh-add &>/dev/null; then | |
| print -u2 "[ssh-agent] Warning: Failed to load SSH keys" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment