Count Firewall rules

All questions related to installations, configurations and maintenance of Advanced Host Monitor (including additional tools such as RMA for Windows, RMA Manager, Web Servie, RCC).
Post Reply
menno
Posts: 158
Joined: Fri May 21, 2010 1:27 am

Count Firewall rules

Post by menno »

Hi there
I am trying to setup a windows firewall rules counter

I am use :

Code: Select all

netsh advfirewall firewall show rule name=all |findstr "Rule Name"  |find /C ":"
the result of this script/cmd line is in my case 5

Can someone please help me to setup a shell script test that verify this value ??
In my case 5 = OK , greater or less than 5 Bad

many many thanks in advance
M
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

You may create D:\scripts\script1.bat file with the following content:

Code: Select all

@echo off
netsh advfirewall firewall show rule name=all |findstr "Rule Name"  |find /C ":"
Shell script test settings:
Start CMD: cmd /c cscript /B /E:VBScript %Script% %Params%

Code: Select all

Option Explicit
const statusUnknown     = "scriptRes:Unknown:"
const statusOk          = "scriptRes:Ok:"
const statusBad         = "scriptRes:Bad:"
dim ws, a, Str
Set ws = CreateObject("WScript.Shell")
Set a = ws.Exec("D:\scripts\script1.bat").StdOut
While Not a.AtEndOfStream
    Str = Str & Trim(a.ReadLine())
Wend

if Str="5" then
  WScript.StdOut.Write statusOk & Str
  else
  WScript.StdOut.Write statusBad & Str
end if
menno
Posts: 158
Joined: Fri May 21, 2010 1:27 am

thanks

Post by menno »

this works...but

can it be done in 1 script ?
if I deploy this test on another servers it can not find this *.bat file...

many thanks in advance
m
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

Option Explicit
const statusUnknown = "scriptRes:Unknown:"
const statusOk = "scriptRes:Ok:"
const statusBad = "scriptRes:Bad:"
dim ws, a, Str
Set ws = CreateObject("WScript.Shell")
Set a = ws.Exec("cmd /c netsh advfirewall firewall show rule name=all |findstr "&chr(34)&"Rule Name"&chr(34)&"|find /C "& chr(34) &":"& chr(34)").StdOut

While Not a.AtEndOfStream
Str = Str & Trim(a.ReadLine())
Wend

if Str="5" then
WScript.StdOut.Write statusOk & Str
else
WScript.StdOut.Write statusBad & Str
end if
menno
Posts: 158
Joined: Fri May 21, 2010 1:27 am

help

Post by menno »

when I create such a test the output says:

Code: Select all

[21:07:49] HostMonitor is going to execute "Firewall rules count locally" script ...
[21:07:49] Script started, no results received
something is wrong here ....please help
maybe there is an other way to count the firewall rules

I only want to see if someone creates one or deletes one because this server is direct on internet and no extra ports must be open (or closed)

therefore I just want to check the total rules.
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

something is wrong here ....please help
Looks like problem with quotes in ws.Exec command.
The following script should work for you:
==========================
Option Explicit
const statusUnknown = "scriptRes:Unknown:"
const statusOk = "scriptRes:Ok:"
const statusBad = "scriptRes:Bad:"
dim ws, a, Str
Set ws = CreateObject("WScript.Shell")
Set a = ws.Exec("cmd /c netsh advfirewall firewall show rule name=all |findstr ""Rule Name"" |find /C "":"" ").StdOut

While Not a.AtEndOfStream
Str = Str & Trim(a.ReadLine())
Wend

if Str="5" then
WScript.StdOut.Write statusOk & Str
else
WScript.StdOut.Write statusBad & Str
end if
==========================
menno
Posts: 158
Joined: Fri May 21, 2010 1:27 am

THX

Post by menno »

I made it possible to use a parameter...
this one is working !!!

Code: Select all

Option Explicit 
 const statusUnknown = "scriptRes:Unknown:" 
 const statusOk = "scriptRes:Ok:" 
 const statusBad = "scriptRes:Bad:" 
 dim ws, a, Str, args, var1 
 Set ws = CreateObject("WScript.Shell") 
 Set a = ws.Exec("cmd /c netsh advfirewall firewall show rule name=all |findstr ""Rule Name"" |find /C "":"" ").StdOut  
 Set args = WScript.Arguments
 var1 = args(0)
 
 While Not a.AtEndOfStream 
 Str = Str & Trim(a.ReadLine()) 
 Wend 

 if Str=var1 then 
 WScript.StdOut.Write statusOk & Str 
 else 
  WScript.StdOut.Write statusBad & Str 
 
 end if 
many many many thanks for ALL the help !!!
m
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

You are welcome!
Post Reply