Skip to content

Commit

Permalink
Add a powershell script with batch helper to update the hosts file wi…
Browse files Browse the repository at this point in the history
…th the WSL ip address.
  • Loading branch information
patrickcarlohickman committed Feb 15, 2023
1 parent 098590e commit 90e5cd0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
41 changes: 41 additions & 0 deletions bin/update-hosts-ips.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@ECHO off

REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM :: Some nice startup stuff
REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SETLOCAL ENABLEEXTENSIONS
SET me=%~n0
SET parent=%~dp0

REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM :: Define the exit codes for the script. Powers of 2 for bitwise work.
REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SET /A errno=0

REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM :: Define main variables for the script.
REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SET ps_script=%me%.ps1

REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM :: Main script logic.
REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -Verb RunAs powershell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -NoLogo -File \"%parent%\%ps_script%\"'"

REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
REM :: Exit paths should point to here.
REM :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:done
IF %errno% NEQ 0 (
ECHO %me%: Script complete with errors.
PAUSE
) ELSE (
ECHO %me%: Script completed successfully.
)

EXIT /B %errno%
39 changes: 39 additions & 0 deletions bin/update-hosts-ips.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#Requires -RunAsAdministrator

# Any host line that contains this TLD will have the IP address updated.
$wslTld = 'test'

$file = $env:windir + '\System32\drivers\etc\hosts'
$regex = '(?<=^\s*)([^\s]+)(?=.*\.' + $wslTld + '(\s+|$))'

# Get the current IP address of the wsl instance.
$wslIp = $(wsl -e hostname -I).Trim()

Write-Host "Updating hosts file for TLD [.$wslTld] with WSL IP [$wslIp]."

# Use the regex to replace existing IP addresses with the
# current wsl IP address for the wsl TLD defined above.
(Get-Content $file) -replace $regex, $wslIp | Set-Content $file

Write-Host 'hosts file updated.'

# If running in the console, wait for input before closing.
if ($Host.Name -eq "ConsoleHost") {
Write-Host ""
Write-Host "Press any key to continue or wait 3 seconds..."

# Make sure buffered input doesn't "press a key" and skip the ReadKey().
$Host.UI.RawUI.FlushInputBuffer()

# Wait for a set time or until a key is pressed.
$counter = 0
while (!$Host.UI.RawUI.KeyAvailable -and $counter -lt 3000) {
Start-Sleep -Milliseconds 100
$counter = $counter + 100
}

# Consume the key if one was pressed.
if ($Host.UI.RawUI.KeyAvailable) {
$Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyUp") > $null
}
}

0 comments on commit 90e5cd0

Please sign in to comment.