Files

69 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
WSL_IP="$(hostname -I | awk '{print $1}')"
WSL_HOST_IP="$(awk '/nameserver/ {print $2; exit}' /etc/resolv.conf)"
WINDOWS_IPS="$(
powershell.exe -NoProfile -Command "(Get-NetIPAddress -AddressFamily IPv4 | Where-Object { \$_.IPAddress -notlike '127.*' -and \$_.IPAddress -notlike '169.254*' -and \$_.InterfaceAlias -notlike '*WSL*' } | Select-Object -ExpandProperty IPAddress) -join ','" \
2>/dev/null | tr -d '\r'
)"
PORT_REPORT="$(ss -ltnH 2>/dev/null | awk '
{
local_addr=$4
n=split(local_addr, parts, ":")
if (n < 2) next
port=parts[n]
host=substr(local_addr, 1, length(local_addr) - length(port) - 1)
gsub(/^\[/, "", host)
gsub(/\]$/, "", host)
key=port SUBSEP host
if (!seen[key]++) {
hosts[port] = hosts[port] ? hosts[port] "," host : host
}
}
END {
for (port in hosts) {
print port "|" hosts[port]
}
}
' | sort -n)"
ACTIVE_PORTS="$(printf '%s\n' "$PORT_REPORT" | awk -F'|' '
NF >= 2 {
if ($2 !~ /(^|,)(127\.0\.0\.1|::1|localhost)(,|$)/ || $2 ~ /(^|,)(0\.0\.0\.0|\*|::)(,|$)/) {
ports = ports ? ports "," $1 : $1
}
}
END { print ports }
')"
LOOPBACK_ONLY_PORTS="$(printf '%s\n' "$PORT_REPORT" | awk -F'|' '
NF >= 2 {
if ($2 ~ /(^|,)(127\.0\.0\.1|::1|localhost)(,|$)/ && $2 !~ /(^|,)(0\.0\.0\.0|\*|::)(,|$)/) {
ports = ports ? ports "," $1 : $1
}
}
END { print ports }
')"
cat <<EOF
WSL 앱 정보
- WSL 내부 IP: ${WSL_IP}
- Windows 호스트와 연결된 주소 추정값: ${WSL_HOST_IP}
- Windows IPv4 후보: ${WINDOWS_IPS:-확인 실패}
- 현재 내부망 공개 가능 포트: ${ACTIVE_PORTS:-없음}
- 루프백 전용 포트: ${LOOPBACK_ONLY_PORTS:-없음}
중요
- WSL2에서는 다른 PC가 WSL 내부 IP(${WSL_IP})로 직접 접속하지 못하는 경우가 많습니다.
- 내부망 다른 PC에서는 Windows 호스트 IP와 각 포트로 접속해야 합니다.
- 따라서 Windows 관리자 PowerShell에서 portproxy + 방화벽 규칙을 설정해야 합니다.
- 루프백 전용 포트(127.0.0.1/::1)는 앱을 0.0.0.0으로 실행해야 내부망 공개가 가능합니다.
다음 단계
1. WSL에서 필요한 앱 실행
2. Windows 관리자 PowerShell 또는 CMD에서 scripts/setup_windows_all_portproxies.cmd 실행
3. 다른 PC 브라우저에서 http://Windows호스트IP:포트 로 접속
EOF