$ErrorActionPreference = "Stop" $key = "C:\Users\juanm\Documents\GitHub\contabo" $vps = "root@185.187.169.109" $remotePort = 18022 $localPort = 2222 if (-not (Test-Path -LiteralPath $key -PathType Leaf)) { throw "No se encuentra la clave SSH del VPS: $key" } $remoteCommand = "if ss -lnt | grep -q ':$remotePort'; then echo TUNNEL_ALREADY_ACTIVE; else nohup ssh -i /root/.ssh/pi_deploy -o BatchMode=yes -o ExitOnForwardFailure=yes -o ServerAliveInterval=30 -p 2222 -N -L $remotePort`:192.168.50.1`:22 pi@127.0.0.1 >/tmp/router-ssh-tunnel.log 2>&1 & echo TUNNEL_STARTED; fi" & ssh -i $key -o BatchMode=yes $vps $remoteCommand $local = Get-NetTCPConnection -LocalPort $localPort -State Listen -ErrorAction SilentlyContinue if (-not $local) { $arguments = @( "-i", $key, "-o", "BatchMode=yes", "-o", "ExitOnForwardFailure=yes", "-N", "-L", "$localPort`:127.0.0.1`:$remotePort", $vps ) Start-Process -FilePath "ssh.exe" -ArgumentList $arguments -WindowStyle Hidden Start-Sleep -Seconds 2 } if (Get-NetTCPConnection -LocalPort $localPort -State Listen -ErrorAction SilentlyContinue) { Write-Output "Tunel activo. Conecta con: ssh -p $localPort usuario_del_router@127.0.0.1" } else { throw "No se pudo abrir el puerto local $localPort" }