Use WSL ssh-agent by default

This commit is contained in:
Fernando Schauenburg 2023-05-05 09:27:10 +02:00
parent ebe8927884
commit b4526f94cc

View file

@ -1,18 +1,20 @@
#!/bin/sh #!/bin/sh
usage () { usage() {
echo "usage: $0 [-w] [-h]" echo "usage: $0 [-w] [-h]"
echo " -w use the WSL ssh-agent instead of OpenSSH for Windows" echo " -w use OpenSSH for Windows instead of WSL ssh-agent"
echo " -h show help and exit" echo " -h show help and exit"
} }
require() { error() {
if ! command -v "$@" >/dev/null 2>&1; then printf "ERROR: %s\n" "$1" >&2
>&2 echo "ERROR: required command not found: $1" exit 1
exit 1
fi
} }
start_with_wsl_agent() { require() {
command -v "$@" >/dev/null 2>&1 || error "'$1' not found"
}
start_wsl_agent() {
if [ "$#" -gt 0 ]; then if [ "$#" -gt 0 ]; then
SHLVL=0 exec ssh-agent "$SHELL" -c "$@" SHLVL=0 exec ssh-agent "$SHELL" -c "$@"
else else
@ -20,15 +22,18 @@ start_with_wsl_agent() {
fi fi
} }
start_with_windows_agent() { start_windows_agent() {
require socat require socat
require npiperelay require npiperelay
# Forward SSH Agent requests from withitn WSL to OpenSSH for Windows, based on: # Forward SSH Agent requests from withitn WSL to OpenSSH for Windows, based on:
# https://stuartleeks.com/posts/wsl-ssh-key-forward-to-windows/ # https://stuartleeks.com/posts/wsl-ssh-key-forward-to-windows/
export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock export SSH_AUTH_SOCK="$HOME/.ssh/agent.sock"
[ -S "$SSH_AUTH_SOCK" ] && rm "$SSH_AUTH_SOCK" [ -S "$SSH_AUTH_SOCK" ] && rm "$SSH_AUTH_SOCK"
socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"npiperelay -ei -s //./pipe/openssh-ssh-agent",nofork & socat \
UNIX-LISTEN:$SSH_AUTH_SOCK,fork \
EXEC:"npiperelay -ei -s //./pipe/openssh-ssh-agent",nofork \
&
if [ "$#" -gt 0 ]; then if [ "$#" -gt 0 ]; then
SHLVL=0 exec "$SHELL" -c "$@" SHLVL=0 exec "$SHELL" -c "$@"
@ -37,32 +42,23 @@ start_with_windows_agent() {
fi fi
} }
# Use OpenSSH for Windows by default. main() {
WSL_SSH_AGENT=false WSL_SSH_AGENT=yes
while getopts ":wh" arg; do
case "$arg" in
w) WSL_SSH_AGENT=no;;
h) usage; exit 0;;
?) usage >&2; error "unknown option -$OPTARG";;
esac
shift
done
while getopts ":wh" arg; do if [ "$WSL_SSH_AGENT" = "yes" ]; then
case "$arg" in start_wsl_agent "$@"
w) else
WSL_SSH_AGENT=true start_windows_agent "$@"
;; fi
}
h) main "$@"
usage
exit 0
;;
?)
>&2 echo "ERROR: unknown option -$OPTARG"
>&2 usage
exit 1
;;
esac
shift
done
if [ "$WSL_SSH_AGENT" = true ]; then
start_with_wsl_agent "$@"
else
start_with_windows_agent "$@"
fi