wslutils/utils/bin/start
2022-09-16 08:55:18 +02:00

68 lines
1.3 KiB
Bash

#!/bin/sh
usage () {
echo "usage: $0 [-w] [-h]"
echo " -w use the WSL ssh-agent instead of OpenSSH for Windows"
echo " -h show help and exit"
}
require() {
if ! command -v "$@" >/dev/null 2>&1; then
>&2 echo "ERROR: required command not found: $1"
exit 1
fi
}
start_with_wsl_agent() {
if [ "$#" -gt 0 ]; then
SHLVL=0 exec ssh-agent "$SHELL" -c "$@"
else
SHLVL=0 exec ssh-agent "$SHELL"
fi
}
start_with_windows_agent() {
require socat
require npiperelay
# Forward SSH Agent requests from withitn WSL to OpenSSH for Windows, based on:
# https://stuartleeks.com/posts/wsl-ssh-key-forward-to-windows/
export SSH_AUTH_SOCK=$HOME/.ssh/agent.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 &
if [ "$#" -gt 0 ]; then
SHLVL=0 exec "$SHELL" -c "$@"
else
SHLVL=0 exec "$SHELL"
fi
}
# Use OpenSSH for Windows by default.
WSL_SSH_AGENT=false
while getopts ":wh" arg; do
case "$arg" in
w)
WSL_SSH_AGENT=true
;;
h)
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