05-sep: [router] automatizar tunel SSH al ASUS

This commit is contained in:
juanjo
2026-09-05 17:48:20 +02:00
parent d897e662d3
commit 5e4b6fbd31
3 changed files with 45 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
$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"
}