From 43a021cc89f40ba58fbdcffc58e14196252a0976 Mon Sep 17 00:00:00 2001 From: Patrick Carlo-Hickman Date: Wed, 5 Apr 2023 14:17:11 -0400 Subject: [PATCH] Update the powershell method for getting the WSL IP address. The previous method could return multiple IP addresses. The new method will specifically get the IP address for the eth0 network adapter. --- bin/add-host.ps1 | 2 +- bin/update-hosts-ips.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/add-host.ps1 b/bin/add-host.ps1 index 9c645aa..5945d36 100644 --- a/bin/add-host.ps1 +++ b/bin/add-host.ps1 @@ -13,7 +13,7 @@ if (!$PSBoundParameters.ContainsKey('wslHost')) { $file = $env:windir + '\System32\drivers\etc\hosts' # Get the current IP address of the wsl instance. -$wslIp = $(wsl -e hostname -I) +$wslIp = $(wsl -e /bin/bash -c "ip -4 -o address show eth0 | grep -oP """"(?<=inet\s)\d+(\.\d+){3}""""") # Read the last byte of the file. $size = 1 diff --git a/bin/update-hosts-ips.ps1 b/bin/update-hosts-ips.ps1 index d393d60..b6e8a45 100644 --- a/bin/update-hosts-ips.ps1 +++ b/bin/update-hosts-ips.ps1 @@ -7,7 +7,7 @@ $file = $env:windir + '\System32\drivers\etc\hosts' $regex = '(?<=^\s*)(\d+\.\d+\.\d+\.\d+)(?=.*\.' + $wslTld + '(\s+|$))' # Get the current IP address of the wsl instance. -$wslIp = $(wsl -e hostname -I).Trim() +$wslIp = $(wsl -e /bin/bash -c "ip -4 -o address show eth0 | grep -oP """"(?<=inet\s)\d+(\.\d+){3}""""") Write-Host "Updating hosts file for TLD [.$wslTld] with WSL IP [$wslIp]."