Skip to content

Instantly share code, notes, and snippets.

@mdimai666
Created October 1, 2025 05:39
Show Gist options
  • Save mdimai666/7a0fcd715df6453a4260177f95119707 to your computer and use it in GitHub Desktop.
Save mdimai666/7a0fcd715df6453a4260177f95119707 to your computer and use it in GitHub Desktop.
Write-Output "---------------------- FIX WSL INTERNET ----------------------"
# 1. Перезапуск WinNAT
# Write-Output "Restarting WinNAT..."
# net stop winnat
# net start winnat
# 5. Завершаем все дистрибутивы WSL
Write-Output "Shutting down WSL..."
wsl --shutdown
# $wslEnthernetAdapterName = "vEthernet (WSL)"
$wslEnthernetAdapterName = "vEthernet (WSL (Hyper-V firewall))"
# 2. Определяем подсеть адаптера "vEthernet (WSL)"
Write-Output "Detecting WSL network..."
$ifconfig = Get-NetIPAddress -InterfaceAlias $wslEnthernetAdapterName -AddressFamily IPv4
if (-not $ifconfig) {
Write-Error "Не найден интерфейс vEthernet (WSL). Проверь, что WSL установлен и запущен хотя бы раз."
exit 1
}
$ip_text = $ifconfig.IPAddress;
Write-Output "IP: $ip_text"
$prefix = $ifconfig.PrefixLength
$ip = $ifconfig[0].IPAddress.Trim()
$octets = $ip -split '\.'
$networkBase = ($octets[0..2] -join '.') + ".0"
$baseIp = [System.Net.IPAddress]::Parse($networkBase)
$subnet = "$($baseIp)/$prefix"
Write-Output "WSL subnet detected: $subnet"
if(1){
# 3. Восстанавливаем NAT
$natName = "WSL"
Write-Output "Re-creating NAT rule..."
Remove-NetNat -Name $natName -Confirm:$false -ErrorAction SilentlyContinue
New-NetNat -Name $natName -InternalIPInterfaceAddressPrefix $subnet
Write-Output "---------------------- DONE ----------------------"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment