天天看点

Windows Server2008通过命令行方式添加防火墙规则

windows server2008增加了一个名叫netsh的命令行工具,通过它可以用批处理方式添加防火墙出入栈规则:

入栈规则:

set port=3389

set rule_name="_远程连接端口:%port% 入栈规则"

netsh advfirewall firewall show rule name=%rule_name% >nul

if not errorlevel 1 (

    rem 对不起,规则 %rulename% 已经存在

) else (

    echo 规则 %rulename% 创建中...

    netsh advfirewall firewall add rule name=%rule_name% dir=in action=allow protocol=tcp localport=%port%

出栈规则:

set rule_name="_远程连接端口:%port% 出栈规则"

    netsh advfirewall firewall add rule name=%rule_name% dir=outaction=allow protocol=tcp localport=%port%

参考:http://stackoverflow.com/questions/15171255/how-to-open-ports-on-windows-firewall-through-batch-file

继续阅读