Last active
February 24, 2026 13: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
| #!/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 | |
| eval "$(command ssh-agent -t 4h)" || { | |
| print -u2 "[ssh-agent] Failed to start custom SSH agent" | |
| return 1 | |
| } | |
| trap 'kill $SSH_AGENT_PID 2>/dev/null' EXIT | |
| 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