□ WSL의 서비스 ip를 외부로 출력 하기 위한 스크립트

 ○ WSL 기동 시 마다 ip가 매번 변하기 때문에 윈도우 방화벽에서 접근을 매번 풀어 주기가 귀찮았는데, 선구자 들의 발견으로 아래와 같은 소스를 공유 합니다.

powershell

$remoteport = bash.exe -c "ifconfig | grep -o -P '(?<=inet).*(?=172.21).*(?=netmask)'"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

if( $found ){
  $remoteport = $matches[0];
} else{
  echo "The Script Exited, the ip address of WSL 2 cannot be found";
  exit;
}

#[Ports]

#All the ports you want to forward separated by coma
$ports=@(80,443,8080);


#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";


#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";

#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";

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

출처 1: https://gist.github.com/xmeng1/aae4b223e9ccc089911ee764928f5486
출처 2: https://github.com/microsoft/WSL/issues/4150#issuecomment-504209723 

.끝  .

 

'IT' 카테고리의 다른 글

Docker setting  (0) 2023.02.12
Coredns (버전 업데이트 자동 대응)  (0) 2022.10.19
영상 다운로드(ffmpeg)  (0) 2022.10.17
Alpine Linux using the korean input method  (0) 2022.10.17
Linux user password encrypted change sh script  (0) 2022.10.07

+ Recent posts