#!/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" } # Use OpenSSH for Windows by default. WSL_SSH_AGENT=false while getopts ":wh" arg; do case "$arg" in w) WSL_SSH_AGENT=true ;; h) >&2 usage exit 0 ;; ?) >&2 echo "ERROR: unknown option -$OPTARG" >&2 usage exit 1 ;; esac shift done if [ "$WSL_SSH_AGENT" = true ]; then if [ "$#" -gt 0 ]; then SHLVL=0 exec ssh-agent "$SHELL" -c "$@" else SHLVL=0 exec ssh-agent "$SHELL" fi fi require() { if ! command -v "$@" >/dev/null 2>&1; then >&2 echo "ERROR: required command not found: $1" exit 1 fi } 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