diff --git a/.scripts/ssh-init b/.scripts/ssh-init new file mode 100755 index 0000000..84db665 --- /dev/null +++ b/.scripts/ssh-init @@ -0,0 +1,27 @@ +#!/bin/sh +# shellcheck disable=SC1091 + +mkdir -p "$HOME/.local/state" +agent_file="$HOME/.local/state/ssh-agent" + +if [ -z "$SSH_AUTH_SOCK" ]; then + if [ -f "$agent_file" ] && [ -r "$agent_file" ]; then + . "$agent_file" > /dev/null 2>&1 + # check if the socket is actually working + if [ "$(ssh-add -l > /dev/null 2>&1; echo $?)" -eq 2 ]; then + unset SSH_AUTH_SOCK + fi + fi + + if [ -z "$SSH_AUTH_SOCK" ]; then + rm -f "$agent_file" + eval "$(ssh-agent -s | tee "$agent_file")" > /dev/null 2>&1 + fi + + [ -f "$agent_file" ] && ( + cat "$agent_file" + exit 0 + ) +fi + +echo "true" \ No newline at end of file diff --git a/fish/post.d/sshs.fish b/fish/post.d/sshs.fish index 7c6b73e..b47947c 100644 --- a/fish/post.d/sshs.fish +++ b/fish/post.d/sshs.fish @@ -1,22 +1,7 @@ # ssh with encrypted private keys # $ssh_keys should be set in advance or left empty to use default keys if type -q ssh - # start ssh agent if not already running - if not set -q SSH_AUTH_SOCK - if test -f "$HOME/.ssh-agent" -a -r "$HOME/.ssh-agent" - bass source "$HOME/.ssh-agent" > /dev/null 2>&1 - # check if the socket is actually working - if not ssh-add -l > /dev/null 2>&1 - command rm -f "$HOME/.ssh-agent" - set -e SSH_AUTH_SOCK - end - end - - if not set -q SSH_AUTH_SOCK 2>&1 - command rm -f "$HOME/.ssh-agent" - bass eval (ssh-agent -s | tee "$HOME/.ssh-agent") > /dev/null 2>&1 - end - end + bass $(ssh-init) > /dev/null 2>&1 # only need to input passphrase once per session function sshs