防火墙网络放行

发布时间 2023-10-18 16:43:10作者: stweily

通常做设置时需要对指定的程序或端口进行防火墙放行

@echo off

rem 设置需要启动的端口
set OTHERPORT=1433,3389

rem 启用防火墙常规端口  可以删除
netsh advfirewall firewall add rule name="_Ping" dir=in protocol=icmpv4 action=allow
netsh advfirewall firewall add rule name="_20 FTP" protocol=TCP dir=in localport=20 action=allow

rem 启用防火墙非常规端口
FOR %%c in (%OTHERPORT%) do (
    SET PORT=%%c
    call :input
    call :output        
    )

pause

rem 入栈规则
:input
set INPUTPORT=%PORT%
set INPUT_RULE_NAME="_%INPUTPORT% 入栈规则"
netsh advfirewall firewall show rule name=%INPUT_RULE_NAME% >nul
if not ERRORLEVEL 1 (
    echo 对不起,规则 %INPUT_RULE_NAME% 已经存在
) else (
    netsh advfirewall firewall add rule name=%INPUT_RULE_NAME% dir=in action=allow protocol=TCP localport=%INPUTPORT%
    echo 规则 %INPUT_RULE_NAME% 创建成功
) 

rem 出栈规则
:output
set OUTPORT=%PORT%
set OUT_RULE_NAME="_%OUTPORT% 出栈规则"
netsh advfirewall firewall show rule name=%OUT_RULE_NAME% >nul
if not ERRORLEVEL 1 (
echo 对不起,规则 %OUT_RULE_NAME% 已经存在
) else (
    netsh advfirewall firewall add rule name=%OUT_RULE_NAME% dir=out action=allow protocol=TCP localport=%OUTPORT%
    echo 规则 %OUT_RULE_NAME% 创建成功
) 

 当然还有简单两句

netsh advfirewall firewall add rule name="AI" dir=in action=allow program="C:\XXX\ai\main.exe" enable=yes
netsh advfirewall firewall add rule name="AI" dir=out action=allow program="C:\XXX\ai\main.exe" enable=yes

 因为比较常见使用,故记录一下