#!/bin/sh usage() { echo "usage: $0 [-w] [-h]" echo " -w use OpenSSH for Windows instead of WSL ssh-agent" echo " -h show help and exit" } error() { printf "ERROR: %s\n" "$1" >&2 exit 1 } require() { command -v "$@" >/dev/null 2>&1 || error "'$1' not found" } start_wsl_agent() { if [ "$#" -gt 0 ]; then SHLVL=0 exec ssh-agent "$SHELL" -c "$@" else SHLVL=0 exec ssh-agent "$SHELL" fi } start_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 } main() { 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 if [ "$WSL_SSH_AGENT" = "yes" ]; then start_wsl_agent "$@" else start_windows_agent "$@" fi } main "$@"