Skip to content

Go mod support and ssh-agent example #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ For example, you can:
* Connect to MySQL Server running as a Windows service
* Connect interactively to a Hyper-V Linux VM's serial console
* Use gdb to connect to debug the kernel of a Hyper-V Linux VM
* Connect to Windows SSH agent via named pipe

Let me know on Twitter ([@gigastarks](https://twitter.com/gigastarks)) if you come up with more interesting uses.

Expand All @@ -27,7 +28,17 @@ Basic steps:

To build the binary, you will need a version of [Go](https://golang.org). You can use a Windows build of Go or, as outlined here, you can use a Linux build and cross-compile the Windows binary directly from WSL.

## Building npiperelay.exe
## Building on Windows

```powershell
git clone https://github.com/jstarks/npiperelay.git
cd npiperelay
go build -o npiperelay.exe
```

Copy `npiperelay.exe` to a location on your path. WSL 2 will read your path and find it.

## Building npiperelay.exe in WSL

Once you have Go installed (and your GOPATH configured), you need to download and install the tool. This is a little tricky because we are building the tool for Windows from WSL:

Expand Down Expand Up @@ -219,6 +230,21 @@ gdb ./vmlinux
target remote /home/<myuser>/foo-pty
```

## Connect to Windows SSH agent

Windows provides [OpenSSH](https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_overview) including `ssh-agent`. If you have [configured](https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_server_configuration) the agent to auto start and added your keys, WSL2 can connect to it using a named pipe via `SSH_AUTH_SOCK`.

Add the following to a `.bashrc` or `.zshrc` configuration to setup WSL `ssh-agent` to use Windows agent.

```bash
export SSH_AUTH_SOCK=${HOME}/.ssh/agent.sock
ss -a | grep -q $SSH_AUTH_SOCK
if [ $? -ne 0 ]; 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
```

## Custom usage

Take a look at the scripts for sample usage, or run `npiperelay.exe` without any parameters for parameter documentation.
9 changes: 9 additions & 0 deletions scripts/ssh-agent

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename this script to ssh-agent-relay to follow convention of other scripts in https://github.com/jstarks/npiperelay/tree/master/scripts

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

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

ss -a | grep -q $SSH_AUTH_SOCK
if [ $? -ne 0 ]; 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