问题:
之前的时候写了一个 win10下如何开放端口 的帖子,是需要手动设置的。但是这样不够快速,于是想通过批处理命令让机器执行。
思路:
使用 netsh advfirewall firewall 命令的 add rule 实现添加规则。
解决:
@echo off
rem 设置指定端口变量和出入站规则名称
set INPUT_RULE_NAME=ZW-IN
set OUT_RULE_NAME=ZW-OUT
set PORT=27700,27701,27702,3316,7998,7999
rem 创建入站规则
echo Input Rule
netsh advfirewall firewall show rule name=%INPUT_RULE_NAME% >nul
rem 如果已经存在则先删除
if not ERRORLEVEL 1 (
netsh advfirewall firewall delete rule name=%INPUT_RULE_NAME% >nul
)
netsh advfirewall firewall add rule name=%INPUT_RULE_NAME% dir=in action=allow protocol=TCP localport=%PORT%
echo %INPUT_RULE_NAME% Create Successed!
pause
rem 创建出站规则
echo Output Rule
netsh advfirewall firewall show rule name=%OUT_RULE_NAME% >nul
rem 如果已经存在则先删除
if not ERRORLEVEL 1 (
netsh advfirewall firewall delete rule name=%OUT_RULE_NAME% >nul
)
netsh advfirewall firewall add rule name=%OUT_RULE_NAME% dir=out action=allow protocol=TCP localport=%PORT%
echo %OUT_RULE_NAME% Create Successed!
echo Done!