1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # ssh.zshrc: Setup (Open-) SSH
4 # Make sure that "ssh(1)" is installed
5 (( $+commands[ssh] )) || return
7 # Load SSH keys into the SSH agent, when one is running and doesn't have
8 # any keys already. Not having an SSH agent running at all is ok as well and
9 # results in an "success" exit code (0) as well.
11 [[ -z "$SSH_AUTH_SOCK" ]] && return 0
12 ssh-add -l >/dev/null && return 0
13 [[ $? -eq 2 ]] && return 2
14 [[ "$1" = "-v" ]] && echo "SSH agent is running, but has no identities."
19 [[ -n "$SSH_CLIENT" ]] || return 1
23 axzsh_logname_prompt_functions=($axzsh_logname_prompt_functions _axzsh_ssh_prompt)
24 axzsh_hostname_prompt_functions=($axzsh_hostname_prompt_functions _axzsh_ssh_prompt)
26 # Validate SSH_AUTH_SOCK: Inside of screen(1) sessions for example, the socket
27 # file becomes invalid when the session has been disconnected.
28 [[ ! -r "$SSH_AUTH_SOCK" ]] && unset SSH_AUTH_SOCK
30 # Look for common socket locations ...
31 if [[ -z "$SSH_AUTH_SOCK" ]]; then
33 /mnt/c/Local/$LOGNAME/ssh-agent.sock
35 if [[ -r "$s" ]]; then
36 export SSH_AUTH_SOCK=$s
43 # Save SSH environment when available:
44 if [[ -n "$SSH_AUTH_SOCK" && -d "$XDG_RUNTIME_DIR" ]]; then
45 # Save current environment when no state exists or state file is invalid.
46 if [[ -r "$XDG_RUNTIME_DIR/ssh-env.sh" ]]; then
47 # Check existing state file ...
49 source "$XDG_RUNTIME_DIR/ssh-env.sh"
50 if [[ -z "$SSH_AUTH_SOCK" || ! -r "$SSH_AUTH_SOCK" ]]; then
51 # Content is invalid, remove state file!
52 rm -f "$XDG_RUNTIME_DIR/ssh-env.sh"
56 if [[ ! -r "$XDG_RUNTIME_DIR/ssh-env.sh" ]]; then
57 # No state file exists, create a new one:
58 echo "SSH_AUTH_SOCK=\"$SSH_AUTH_SOCK\"" >"$XDG_RUNTIME_DIR/ssh-env.sh"
59 echo "export SSH_AUTH_SOCK" >>"$XDG_RUNTIME_DIR/ssh-env.sh"
63 # Restore SSH environment when not set but available:
64 if [[ -z "$SSH_AUTH_SOCK" && -r "$XDG_RUNTIME_DIR/ssh-env.sh" ]]; then
65 # Try to recover known good environment ...
66 source "$XDG_RUNTIME_DIR/ssh-env.sh"
67 if [[ ! -r "$SSH_AUTH_SOCK" ]]; then
70 rm -f "$XDG_RUNTIME_DIR/ssh-env.sh"