當我跑
PS C:\WINDOWS\system32> wsl hostname -I
172.27.96.44
PS C:\WINDOWS\system32> netsh interface portproxy add v4tov4 listenport=3000 connectport=3000 connectaddress=172.27.96.44
PS C:\WINDOWS\system32> netsh interface portproxy show all
Listen on ipv4: Connect to ipv4:
Address Port Address Port
--------------- ---------- --------------- ----------
* 3000 172.17.218.173 3000
它有效,目標地址是可訪問的。但是當我跑
netsh interface portproxy add v4tov4 listenport=3000 connectport=3000 connectaddress=$(wsl hostname -I)
PS C:\WINDOWS\system32> netsh interface portproxy show all
Listen on ipv4: Connect to ipv4:
Address Port Address Port
--------------- ---------- --------------- ----------
* 3000 172.27.96.44 3000
目標地址不可訪問。它只是沒有反應。盡管它顯示在表中。我嘗試過字串轉換,例如
netsh interface portproxy add v4tov4 listenport=3000 connectport=3000 connectaddress=([string] (wsl hostname -I));
但它沒有幫助。為什么即使ip地址在表中也不起作用?
uj5u.com熱心網友回復:
如果您獲取 WSL 命令的輸出和您認為相同值的逐字字串,并將實際位元組與 比較Format-Hex,您會發現存在差異。
$output = WSL hostname -I
$output | Format-Hex
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 31 39 32 2E 31 36 38 2E 31 2E 36 38 20 192.168.1.68
VS
'192.168.1.68' | Format-Hex
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 31 39 32 2E 31 36 38 2E 31 2E 36 38 192.168.1.68
所以它增加了一個值,一個 20。我猜了一下,它確實是一個空格。
$output -replace '\s' | Format-Hex
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 31 39 32 2E 31 36 38 2E 31 2E 36 38 192.168.1.68
因此,您可以通過各種方式解決此問題,但如圖所示,它可以很簡單。
$output = (WSL hostname -I) -replace '\s'
您始終可以測驗變數的內容。我經常做這樣的事情只是為了檢查一下。它肯定會在這里救你。
$output = WSL hostname -I
Write-Host "A$($output)A"
A192.168.1.68 A
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/323340.html
