我的设置 WSL 2 SSH 端口转发的脚本失败

我的设置 WSL 2 SSH 端口转发的脚本失败

我以前可以使用 SSH 脚本并将 IP 设置为 127.0.0.2 来连接,但现在每次启动时都无法连接。我不知道为什么。我尝试了 localhost 或 127.0.0.1,但不起作用。我可以在 localhost 上访问网站。如何在我自己的托管 WSL2 的机器上获取通过 SecureCRT 连接的端口号和 IP?

try {
## Port forward WSL virtual machine ports to host firewall for remote access
## !! this script must be run as Administrator !!
##  powershell -executionpolicy bypass -file "c:\docker\WSL-portproxy.ps1"

## ip -o -4 -f inet addr show eth0 | awk '{ split($4, ip_addr, "/"); print ip_addr[1]; }'
##   print out is "4:  eth0  inet  172.20.x.x/20  brd  ..." string
$remoteport = bash.exe -c "ip -o -4 -f inet addr show eth0";
$remoteport = ($remoteport -split "\s+")[3];
$remoteport = $remoteport.substring(0, $remoteport.LastIndexOf("/"));
$found      = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if(!$found){
  echo "WSL ip address not found";
  exit;
}
echo "WSL ip address $remoteport";

# Port forwards from WSL virtual machine to Win10 host firewall
# Bind a specific host ip addr or use 0.0.0.0 default
$ports=@(2222);
$addr='127.0.0.2';

# Remove firewall exception rules, add inbound and outbound rules
$ports_a = $ports -join ",";
echo "Firewall inbound/outbound rules";
iex "Remove-NetFireWallRule -DisplayName 'WSL Firewall Unlock' ";
iex "New-NetFireWallRule -DisplayName 'WSL Firewall Unlock' -Description 'Remote access to WSL services' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL Firewall Unlock' -Description 'Remote access to WSL services' -Direction Inbound  -LocalPort $ports_a -Action Allow -Protocol TCP";

for( $i = 0; $i -lt $ports.length; $i++ ){
  $port = $ports[$i];
  echo "portproxy ${addr}:${port} to ${remoteport}:${port}";
  iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
  iex "netsh interface portproxy add    v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}

    # Do your script's stuff
}
catch
{
    Write-Error $_.Exception.ToString()
    Read-Host -Prompt "The above error occurred. Press Enter to exit."
}

可以使用这个来设置它,但它不再起作用。

我收到以下错误:

C:\Users\admin\Downloads\f.ps1 : System.Management.Automation.RuntimeException: You cannot call a method on a
null-valued expression.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception
exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
At line:1 char:91
+ ... tionPolicy -Scope Process Bypass }; & 'C:\Users\admin\Downloads\f.ps1'
+                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,f.ps1

答案1

如何在我自己的托管 WSL2 的机器上获取通过 SecureCRT 可以连接的端口号和 IP?

也许我严重误解了你的问题,但是这个应该非常简单,根本不需要脚本。

如果您正在连接:

  • 托管 WSL2 的计算机上的 SecureCRT
  • 该机器上的 WSL2 实例

然后 WSL2 提供了一个称为“localhost 转发”的功能(自 build 18945 以来,根据本网站

您使用的脚本显然非常老旧,不仅因为您不再需要它,还因为它使用的命令几年前也bash.exe被更灵活的命令所取代。有一段时间,微软将其列为“历史命令”,但在最新的文档中wsl.exebash.exe它被标记为已弃用

无论如何,关于你的核心问题,根据WSL 文档

如果您在 Linux 发行版中构建网络应用程序(例如在 NodeJS 或 SQL 服务器上运行的应用程序),则可以使用 localhost 从 Windows 应用程序(如 Edge 或 Chrome 网络浏览器)访问它(就像平常一样)。

但是,如果您运行的是旧版本的 Windows(Build 18945 或更低版本),则需要获取 Linux 主机 VM 的 IP 地址(或更新到最新的 Windows 版本)。

这不仅适用于“构建应用程序”,也适用于现有应用程序/服务,例如 SSH。

这意味着,当您尝试连接到localhostWindows 中的端口时,如果该端口未受 Windows 本身中的应用程序/服务的绑定,那么它会自动尝试将连接转发到 WSL2 中的该端口(任何和所有实例,因为它们共享相同的网络堆栈)。

所以实际上,如果你的 WSL2 实例在端口 2222 上运行 SSH,那么你应该只需从 SecureCRT 连接即可,localhost:2222无需任何额外努力。

有几个原因可能导致这种情况不是工作:

  • 如果你有一个%userprofile%\.wslconfig文件本地主机转发明确设置为false(不太可能)

  • 众所周知,每当 Windows 休眠时,本地主机转发都会停止工作。休眠也可能被“快速启动”选项(Windows 中的默认选项)无意中触发。请参阅我在 Stack Overflow 上的回答了解更多信息。

除此之外,它(再次)应该无需任何复杂的转发脚本即可工作。

相关内容