How to open ports on Windows firewall through batch file

前端 未结 2 1592
暗喜
暗喜 2021-02-05 14:21

Is there any way within a batch file to open up specific ports on Windows through a batch file? It would be nice to have the installer do this for our server application rather

2条回答
  •  攒了一身酷
    2021-02-05 14:54

    This is an extension of solution provided by @Kevin Richardson. Note that "netsh advfirewall add rule" command will create a new rule with the same name every time you run the same command. The script below helps to prevent it

    ECHO OFF
    set PORT=8081
    set RULE_NAME="Open Port %PORT%"
    
    netsh advfirewall firewall show rule name=%RULE_NAME% >nul
    if not ERRORLEVEL 1 (
        rem Rule %RULE_NAME% already exists.
        echo Hey, you already got a out rule by that name, you cannot put another one in!
    ) else (
        echo Rule %RULE_NAME% does not exist. Creating...
        netsh advfirewall firewall add rule name=%RULE_NAME% dir=in action=allow protocol=TCP localport=%PORT%
    )
    

提交回复
热议问题