Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active October 27, 2025 14:24
Show Gist options
  • Save Jaykul/19e9f18b8a68f6ab854e338f9b38ca7b to your computer and use it in GitHub Desktop.
Save Jaykul/19e9f18b8a68f6ab854e338f9b38ca7b to your computer and use it in GitHub Desktop.

Revisions

  1. Jaykul revised this gist May 2, 2023. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions Install.ps1
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <#
    .SYNOPSIS
    Install npiperelay and socat and convfigure SSH_AUTH_SOCK forwarding
    .SYNOPSIS
    Install npiperelay and socat and configure SSH_AUTH_SOCK forwarding
    #>
    [CmdletBinding()]
    param(
    @@ -12,33 +12,33 @@ param(
    # Defaults to your username all lowercase
    [Parameter(ParameterSetName = "Insecure")]
    $Username = $Env:USERNAME.ToLower(),

    # Ingore chocolatey for install (winget must be available).
    [switch]$NoChocolate
    )
    # Install npiperelay
    if (!(Get-Command npiperelay.exe -ErrorAction Ignore)) {
    if (-not $NoChocolate -and (Get-Command choco -ErrorAction Ignore)) {
    choco upgrade npiperelay -y
    } elseif (Get-Command winget -ErrorAction Ignore) {
    winget install --id=jstarks.npiperelay -e --accept-source-agreements
    } else {
    throw "Unable to install. Please download https://github.com/jstarks/npiperelay/releases/latest/download/npiperelay_windows_amd64.zip and extract it somewhere in your PATH"
    }
    if (-not $NoChocolate -and (Get-Command choco -ErrorAction Ignore)) {
    choco upgrade npiperelay -y
    } elseif (Get-Command winget -ErrorAction Ignore) {
    winget install --id=jstarks.npiperelay -e --accept-source-agreements
    } else {
    throw "Unable to install. Please download https://github.com/jstarks/npiperelay/releases/latest/download/npiperelay_windows_amd64.zip and extract it somewhere in your PATH"
    }
    }

    # install socat in WSL
    wsl -d $Distribution -u root apt install socat
    if ($LASTEXITCODE) {
    throw "Unable to install socat. I give up."
    throw "Unable to install socat. I give up."
    }

    # create the ssh-agent-pipe script in WSL
    # Ensure the carriage returns are correct (and fetch the script, if necessary):
    $script = if (Test-Path $PSScriptRoot\ssh-agent-pipe.sh) {
    (Get-Content $PSScriptRoot\ssh-agent-pipe.sh) -join "`n"
    (Get-Content $PSScriptRoot\ssh-agent-pipe.sh) -join "`n"
    } else {
    Invoke-RestMethod https://gist.github.com/Jaykul/19e9f18b8a68f6ab854e338f9b38ca7b/raw/ssh-agent-pipe.sh
    Invoke-RestMethod https://gist.github.com/Jaykul/19e9f18b8a68f6ab854e338f9b38ca7b/raw/ssh-agent-pipe.sh
    }
    # escape $ and " so we can pass this through bash
    $script = $script -replace "\$", "\$" -replace '"', '\"'
  2. Jaykul revised this gist May 2, 2023. No changes.
  3. Jaykul revised this gist May 2, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Install.ps1
    Original file line number Diff line number Diff line change
    @@ -49,4 +49,4 @@ wsl -d $Distribution -u root -- bash -c "cat > /usr/local/bin/ssh-agent-pipe <<'
    wsl -d $Distribution -u root chmod +x /usr/local/bin/ssh-agent-pipe

    # Add to .bashrc for the specified user
    wsl -d $Distribution -u $Username -- bash -c "echo \"source /usr/local/bin/ssh-agent-pipe\" >> ~/.bashrc"
    wsl -d $Distribution -u $Username -- bash -c "echo \`"source /usr/local/bin/ssh-agent-pipe\`" >> ~/.bashrc"
  4. Jaykul revised this gist May 2, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Install.ps1
    Original file line number Diff line number Diff line change
    @@ -49,4 +49,4 @@ wsl -d $Distribution -u root -- bash -c "cat > /usr/local/bin/ssh-agent-pipe <<'
    wsl -d $Distribution -u root chmod +x /usr/local/bin/ssh-agent-pipe

    # Add to .bashrc for the specified user
    wsl -d $Distribution -u $Username -- bash -c "echo \"source /usr/local/bin/ssh-agent-pipe\" >> ~/.bashrc'
    wsl -d $Distribution -u $Username -- bash -c "echo \"source /usr/local/bin/ssh-agent-pipe\" >> ~/.bashrc"
  5. Jaykul revised this gist May 2, 2023. 2 changed files with 4 additions and 4 deletions.
    6 changes: 3 additions & 3 deletions Agent Passthru.md
    Original file line number Diff line number Diff line change
    @@ -5,15 +5,15 @@ I tried to point someone else at it and they were very confused by it, so this i

    # Installation

    With [Chocolatey](https://community.chocolatey.org/), you must use an elevated PowerShell session. If there's no `choco` command found, it will fall back to winget for the npiperelay install.
    With [Chocolatey](https://community.chocolatey.org/), you must use an elevated PowerShell session. If there's no `choco` command found, it will fall back to winget for the npiperelay install. To force using Winget even if you have choco installed, you need to download it, so you can pass parameters to it.

    Just run this in PowerShell:
    Easy mode: just run this in PowerShell:

    ```powershell
    iex (irm https://gist.github.com/Jaykul/19e9f18b8a68f6ab854e338f9b38ca7b/raw/Install.ps1)
    ```

    To be more cautious, click the [Download zip](#) button in the top right, unzip everything, read it through carefully, to make sure I'm not tricking you (or switch the install to winget) and _then_ run the `Install.ps1` script.
    To be more cautious, click the [Download zip](https://gist.github.com/Jaykul/19e9f18b8a68f6ab854e338f9b38ca7b/archive/7249d778915993e1358058b40a0c6adeafe163da.zip) button in the top right, unzip everything, read it through carefully, to make sure I'm not tricking you. _Then_ run the `Install.ps1` script from the unzipped files.

    ## What does it do?

    2 changes: 1 addition & 1 deletion Install.ps1
    Original file line number Diff line number Diff line change
    @@ -49,4 +49,4 @@ wsl -d $Distribution -u root -- bash -c "cat > /usr/local/bin/ssh-agent-pipe <<'
    wsl -d $Distribution -u root chmod +x /usr/local/bin/ssh-agent-pipe

    # Add to .bashrc for the specified user
    wsl -d $Distribution -u $Username -- bash -c 'echo "source /usr/local/bin/ssh-agent-pipe" >> ~/.bashrc'
    wsl -d $Distribution -u $Username -- bash -c "echo \"source /usr/local/bin/ssh-agent-pipe\" >> ~/.bashrc'
  6. Jaykul revised this gist May 2, 2023. 2 changed files with 37 additions and 8 deletions.
    20 changes: 18 additions & 2 deletions Agent Passthru.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,22 @@ I tried to point someone else at it and they were very confused by it, so this i

    # Installation

    Either copy and paste the contents of the Install.ps1 into PowerShell _on Windows_, or download the zip of this, extract both script files, and run the install.ps1 from PowerShell (on Windows)
    With [Chocolatey](https://community.chocolatey.org/), you must use an elevated PowerShell session. If there's no `choco` command found, it will fall back to winget for the npiperelay install.

    wsl-ssh-agent
    Just run this in PowerShell:

    ```powershell
    iex (irm https://gist.github.com/Jaykul/19e9f18b8a68f6ab854e338f9b38ca7b/raw/Install.ps1)
    ```

    To be more cautious, click the [Download zip](#) button in the top right, unzip everything, read it through carefully, to make sure I'm not tricking you (or switch the install to winget) and _then_ run the `Install.ps1` script.

    ## What does it do?

    It's all designed to be run from PowerShell (5+) on Windows. When commands need to be run in WSL, it will use the `wsl` command to do so (as `root` and as _you_). Before you start, make sure you have wsl 2 and a distro installed (run `wsl --list -v` and pick a distro that has VERSION 2).

    1. Install npiperelay in Windows using chocolatey or winget
    2. Install socat in WSL using apt (sorry, if your distro isn't apt, please fork and comment 😉)
    3. Copy an `ssh-agent-pipe` script that starts npiperelay for you whenever you open bash
    4. Make that script executable
    5. Add that script to your .bashrc (note: better not to run this install multiple times 😝)
    25 changes: 19 additions & 6 deletions Install.ps1
    Original file line number Diff line number Diff line change
    @@ -11,14 +11,27 @@ param(
    # The user for whom .bashrc should be modified
    # Defaults to your username all lowercase
    [Parameter(ParameterSetName = "Insecure")]
    $Username = $Env:USERNAME.ToLower()
    $Username = $Env:USERNAME.ToLower(),

    # Ingore chocolatey for install (winget must be available).
    [switch]$NoChocolate
    )
    # Install npiperelay with chocolatey.
    # You could, instead, use: winget install --id=jstarks.npiperelay -e
    choco upgrade npiperelay -y
    # Install npiperelay
    if (!(Get-Command npiperelay.exe -ErrorAction Ignore)) {
    if (-not $NoChocolate -and (Get-Command choco -ErrorAction Ignore)) {
    choco upgrade npiperelay -y
    } elseif (Get-Command winget -ErrorAction Ignore) {
    winget install --id=jstarks.npiperelay -e --accept-source-agreements
    } else {
    throw "Unable to install. Please download https://github.com/jstarks/npiperelay/releases/latest/download/npiperelay_windows_amd64.zip and extract it somewhere in your PATH"
    }
    }

    # install socat in WSL
    wsl -d $Distribution -u root apt install socat
    if ($LASTEXITCODE) {
    throw "Unable to install socat. I give up."
    }

    # create the ssh-agent-pipe script in WSL
    # Ensure the carriage returns are correct (and fetch the script, if necessary):
    @@ -27,8 +40,8 @@ $script = if (Test-Path $PSScriptRoot\ssh-agent-pipe.sh) {
    } else {
    Invoke-RestMethod https://gist.github.com/Jaykul/19e9f18b8a68f6ab854e338f9b38ca7b/raw/ssh-agent-pipe.sh
    }
    # escape $ so we can pass this through bash
    $script = $script -replace "\$", "\$"
    # escape $ and " so we can pass this through bash
    $script = $script -replace "\$", "\$" -replace '"', '\"'

    wsl -d $Distribution -u root -- bash -c "cat > /usr/local/bin/ssh-agent-pipe <<'EOF'`n${script}`nEOF"

  7. Jaykul revised this gist May 2, 2023. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion Install.ps1
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ $script = if (Test-Path $PSScriptRoot\ssh-agent-pipe.sh) {
    # escape $ so we can pass this through bash
    $script = $script -replace "\$", "\$"

    wsl -d $Distribution -u root -- bash -c "cat > /usr/local/bin/ssh-agent-pipe <<'EOF'`n$script`nEOF"
    wsl -d $Distribution -u root -- bash -c "cat > /usr/local/bin/ssh-agent-pipe <<'EOF'`n${script}`nEOF"

    # Make it executable
    wsl -d $Distribution -u root chmod +x /usr/local/bin/ssh-agent-pipe
    2 changes: 1 addition & 1 deletion ssh-agent-pipe.sh
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ if [ "$1" = "-k" ] || [ "$1" = "-r" ]; then
    if [ -n "${sshpid}" ]; then
    kill "${sshpid}"
    else
    echo "'socat' not found or PID not found"
    echo "socat not found or PID not found"
    fi
    if [ "$1" = "-k" ]; then
    exit
  8. Jaykul revised this gist May 2, 2023. 3 changed files with 21 additions and 10 deletions.
    2 changes: 1 addition & 1 deletion Agent Passthru.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,6 @@ I tried to point someone else at it and they were very confused by it, so this i

    # Installation

    Download the zip of this, and run the install.ps1
    Either copy and paste the contents of the Install.ps1 into PowerShell _on Windows_, or download the zip of this, extract both script files, and run the install.ps1 from PowerShell (on Windows)

    wsl-ssh-agent
    27 changes: 19 additions & 8 deletions Install.ps1
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,38 @@
    <#
    .SYNOPSIS
    Install npiperelay and socat and convfigure SSH_AUTH_SOCK forwarding
    #>
    [CmdletBinding()]
    param(
    # The distribution to connect the pipe to
    [Parameter(Position = 0)]
    $Distribution = "ubuntu",
    $Distribution = "Ubuntu",

    # Optionally, a user (startiong the pipe will be added to their .bashrc)
    # Defaults to your username all lowercase (as in the other functions here)
    # The user for whom .bashrc should be modified
    # Defaults to your username all lowercase
    [Parameter(ParameterSetName = "Insecure")]
    $Username = $Env:USERNAME.ToLower()
    )
    # install npiperelay
    # Install npiperelay with chocolatey.
    # You could, instead, use: winget install --id=jstarks.npiperelay -e
    choco upgrade npiperelay -y

    # install socat in WSL
    wsl -d $Distribution -u root apt install socat

    # create the ssh-agent-pipe script in WSL
    # Ensure the carriage returns are correct, and escape $ for bash
    # Ensure the carriage returns are correct (and fetch the script, if necessary):
    $script = if (Test-Path $PSScriptRoot\ssh-agent-pipe.sh) {
    (Get-Content $PSScriptRoot\ssh-agent-pipe.sh) -join "`n" -replace "\$", "\$"
    (Get-Content $PSScriptRoot\ssh-agent-pipe.sh) -join "`n"
    } else {

    Invoke-RestMethod https://gist.github.com/Jaykul/19e9f18b8a68f6ab854e338f9b38ca7b/raw/ssh-agent-pipe.sh
    }
    # escape $ so we can pass this through bash
    $script = $script -replace "\$", "\$"

    wsl -d $Distribution -u root -- bash -c "cat > /usr/local/bin/ssh-agent-pipe <<'EOF'`n$script`nEOF"
    # Set the permissions

    # Make it executable
    wsl -d $Distribution -u root chmod +x /usr/local/bin/ssh-agent-pipe

    # Add to .bashrc for the specified user
    2 changes: 1 addition & 1 deletion ssh-agent-pipe.sh
    Original file line number Diff line number Diff line change
    @@ -25,4 +25,4 @@ fi
    if [ -z "${sshpid}" ]; then
    rm -f $SSH_AUTH_SOCK
    ( setsid socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork & ) >/dev/null 2>&1
    fi
    fi
  9. Jaykul created this gist May 2, 2023.
    10 changes: 10 additions & 0 deletions Agent Passthru.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    For awhile now, each time I got a new Windows laptop
    I would dig up [strasis gist](https://gist.github.com/strarsis/e533f4bca5ae158481bbe53185848d49) on
    how to set up agent forwarding for SSH in WSL2 -- but recently
    I tried to point someone else at it and they were very confused by it, so this is my attempt at simpler instructions.

    # Installation

    Download the zip of this, and run the install.ps1

    wsl-ssh-agent
    28 changes: 28 additions & 0 deletions Install.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    [CmdletBinding()]
    param(
    # The distribution to connect the pipe to
    [Parameter(Position = 0)]
    $Distribution = "ubuntu",

    # Optionally, a user (startiong the pipe will be added to their .bashrc)
    # Defaults to your username all lowercase (as in the other functions here)
    [Parameter(ParameterSetName = "Insecure")]
    $Username = $Env:USERNAME.ToLower()
    )
    # install npiperelay
    choco upgrade npiperelay -y
    # install socat in WSL
    wsl -d $Distribution -u root apt install socat
    # create the ssh-agent-pipe script in WSL
    # Ensure the carriage returns are correct, and escape $ for bash
    $script = if (Test-Path $PSScriptRoot\ssh-agent-pipe.sh) {
    (Get-Content $PSScriptRoot\ssh-agent-pipe.sh) -join "`n" -replace "\$", "\$"
    } else {

    }
    wsl -d $Distribution -u root -- bash -c "cat > /usr/local/bin/ssh-agent-pipe <<'EOF'`n$script`nEOF"
    # Set the permissions
    wsl -d $Distribution -u root chmod +x /usr/local/bin/ssh-agent-pipe

    # Add to .bashrc for the specified user
    wsl -d $Distribution -u $Username -- bash -c 'echo "source /usr/local/bin/ssh-agent-pipe" >> ~/.bashrc'
    28 changes: 28 additions & 0 deletions ssh-agent-pipe.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/bin/bash
    # Usage: ssh-agent-pipe [ -k | -r ]
    # Options:
    # -k Kill the current process (if exists) and do not restart it.
    # -r Kill the current process (if exists) and restart it.
    # Default operation is to start a process only if it does not exist.

    export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock

    sshpid=$(ss -ap | grep "$SSH_AUTH_SOCK")
    if [ "$1" = "-k" ] || [ "$1" = "-r" ]; then
    sshpid=${sshpid//*pid=/}
    sshpid=${sshpid%%,*}
    if [ -n "${sshpid}" ]; then
    kill "${sshpid}"
    else
    echo "'socat' not found or PID not found"
    fi
    if [ "$1" = "-k" ]; then
    exit
    fi
    unset sshpid
    fi

    if [ -z "${sshpid}" ]; then
    rm -f $SSH_AUTH_SOCK
    ( setsid socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork & ) >/dev/null 2>&1
    fi