From d6e7f411518a43bd8bd629a0f1db5ca747319979 Mon Sep 17 00:00:00 2001 From: mattn Date: Sun, 26 Jun 2022 14:26:21 +0900 Subject: [PATCH] cmd/tailscale: make ssh command prefer Windows ssh.exe over PATH use C:\Windows\System32\OpenSSH\ssh.exe (#4933) Change-Id: Iad73b27b23b746de2c7e994572a54e7e1c370588 Signed-off-by: Yasuhiro Matsumoto (cherry picked from commit 1d04e01d1e62e0f83a24c0e2963c1b74db33c00d) --- cmd/tailscale/cli/ssh.go | 3 +-- cmd/tailscale/cli/ssh_exec.go | 5 +++++ cmd/tailscale/cli/ssh_exec_js.go | 4 ++++ cmd/tailscale/cli/ssh_exec_windows.go | 13 +++++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cmd/tailscale/cli/ssh.go b/cmd/tailscale/cli/ssh.go index 92bd42a10..0db6ebd51 100644 --- a/cmd/tailscale/cli/ssh.go +++ b/cmd/tailscale/cli/ssh.go @@ -11,7 +11,6 @@ "fmt" "log" "os" - "os/exec" "os/user" "path/filepath" "runtime" @@ -63,7 +62,7 @@ func runSSH(ctx context.Context, args []string) error { hostForSSH = v } - ssh, err := exec.LookPath("ssh") + ssh, err := findSSH() if err != nil { // TODO(bradfitz): use Go's crypto/ssh client instead // of failing. But for now: diff --git a/cmd/tailscale/cli/ssh_exec.go b/cmd/tailscale/cli/ssh_exec.go index 02fbde61f..bee4d3242 100644 --- a/cmd/tailscale/cli/ssh_exec.go +++ b/cmd/tailscale/cli/ssh_exec.go @@ -10,9 +10,14 @@ import ( "errors" "os" + "os/exec" "syscall" ) +func findSSH() (string, error) { + return exec.LookPath("ssh") +} + func execSSH(ssh string, argv []string) error { if err := syscall.Exec(ssh, argv, os.Environ()); err != nil { return err diff --git a/cmd/tailscale/cli/ssh_exec_js.go b/cmd/tailscale/cli/ssh_exec_js.go index bc1a97e7c..3dbbea6a2 100644 --- a/cmd/tailscale/cli/ssh_exec_js.go +++ b/cmd/tailscale/cli/ssh_exec_js.go @@ -8,6 +8,10 @@ "errors" ) +func findSSH() (string, error) { + return "", errors.New("Not implemented") +} + func execSSH(ssh string, argv []string) error { return errors.New("Not implemented") } diff --git a/cmd/tailscale/cli/ssh_exec_windows.go b/cmd/tailscale/cli/ssh_exec_windows.go index 1905fff39..88722734f 100644 --- a/cmd/tailscale/cli/ssh_exec_windows.go +++ b/cmd/tailscale/cli/ssh_exec_windows.go @@ -8,8 +8,21 @@ "errors" "os" "os/exec" + "path/filepath" ) +func findSSH() (string, error) { + // use C:\Windows\System32\OpenSSH\ssh.exe since unexpected behavior + // occured with ssh.exe provided by msys2/cygwin and other environments. + if systemRoot := os.Getenv("SystemRoot"); systemRoot != "" { + exe := filepath.Join(systemRoot, "System32", "OpenSSH", "ssh.exe") + if st, err := os.Stat(exe); err == nil && !st.IsDir() { + return exe, nil + } + } + return exec.LookPath("ssh") +} + func execSSH(ssh string, argv []string) error { // Don't use syscall.Exec on Windows, it's not fully implemented. cmd := exec.Command(ssh, argv[1:]...)