Skip to content

Commit 1ad5804

Browse files
committed
Use net module instead of checking string prefix
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 2bee694 commit 1ad5804

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

pkg/driver/wsl2/vm_windows.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,12 @@ func getSSHAddress(ctx context.Context, instName string) (string, error) {
282282
// fallback
283283
cmd = exec.CommandContext(ctx, "wsl.exe", "-d", distroName, "hostname", "-i")
284284
out, err = cmd.CombinedOutput()
285-
if err != nil || strings.HasPrefix(string(out), "127.") {
286-
return "", fmt.Errorf("failed to get hostname for instance %q, err: %w (out=%q)", instName, err, string(out))
285+
if err == nil {
286+
ip := net.ParseIP(strings.TrimSpace(string(out)))
287+
// some distributions use "127.0.1.1" as the host IP, but we want something that we can route to here
288+
if ip != nil && !ip.IsLoopback() {
289+
return strings.TrimSpace(string(out)), nil
290+
}
287291
}
288-
289-
return strings.TrimSpace(string(out)), nil
292+
return "", fmt.Errorf("failed to get hostname for instance %q, err: %w (out=%q)", instName, err, string(out))
290293
}

0 commit comments

Comments
 (0)