320x100
320x100

1. 방화벽 설정을 위한 파워셸 스크립트 작성 (.ps1 파일)

$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$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, 8080, 443);


#[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";
}

: 메모장에 위 코드를 붙혀넣기 하고 $ports의 괄호안 부분에 WSL 환경에 대해 개방할 포트 번호를 넣고 저장

: 저장한 .txt 파일 우클릭 > 이름 바꾸기 > 확장자를 .ps1으로 변경

 

 

 

 

 

2. 윈도 작업 스케줄러 실행 및 작업 스케줄러 폴더 생성

시작 > 실행 > taskschd.msc

작업 스케줄러 라이브러리 > 새폴더 > 폴더 생성

 

 

 

 

 

3. 스케줄러에 대한 일반탭 설정

만든 폴더 > 작업 만들기 > 일반탭 > 

이름 / 사용자의 로그온 여부에 관계없이 실행 체크, 가장 높은 수준의 권한으로 실행 체크

 

 

 

 

 

4. 트리거 설정 (실행 조건 설정)

트리거탭 > 새로만들기 > 작업 시작 (로그온 할때) > 특정 사용자 (주 사용 계정 선택)

 

 

 

 

 

5. 스케줄러의 동작 명령 설정

동작탭 > 새로만들기 > 동작 (프로그램 시작)

 > 프로그램/스크립트(P): C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

 > 인수추가 옵션 : -ExecutionPolicy Unrestricted -File "C:\파일경로\파일명.ps1"

 

 

 

 

6. 테스트

: 작업스케줄러에서 만들어진 스케줄 클릭 후 실행 클릭

: 실행중이라고 나오면 정상

: 이후 재부팅 후 정상적으로 포트가 열리는지 확인

300x250
728x90