@echo off
setlocal
:: 设置需要开放的端口号
set PORT=****
:: 设置规则名称
set RULENAME=Allow_%PORT%
:: 添加防火墙规则
netsh advfirewall firewall add rule name="%RULENAME%" dir=in action=allow protocol=tcp localport=%PORT%
echo Port %PORT% has been added to Windows Firewall Inbound Rules.
endlocal
参数解释:
-
name:规则名称
-
dir:方向,in为入站,out为出站
-
localport:指定端口号
-
action:操作,allow允许连接,block阻止连接
-
protocol:协议类型 (tcp/udp)