ports check in windows

March 10, 2011 at 08:37:45
Specs: Windows XP
Hi, I just wanted to see if anyone has done anything what I am trying to do. I need bat script that checks to see if a specific port # is currently listening on a given server or not. I can go as for as netstat -an | find "2222" but this command alone won't do much.

New to batch scripting in windows so any help would be greatly appreciated.

See More: ports check in windows

Report •

March 10, 2011 at 12:50:33
i would say for port scanning its better to use a IP scanner like NMAP than Batch..

Subhash Chandra.

Report •

March 11, 2011 at 08:02:27
Thanks for your reply Subhash, but I would a batch script across many servers that would tell me if the specified port is running or not on a given host.

Report •

March 11, 2011 at 14:12:56
What would you like to happen if the port is or is not found?

Is there an application or service that should be ran if the port isn't found?

A message sent to some other computer or email?

If you're looking to run this from your own machine against many different servers you can try using psexec (make sure you have it downloaded and in your local path):

@echo off
echo %date% %time% >report.txt
set portnum=222
for /f %%a in (servers.txt) do (
psexec \\%%a netstat -an | find "%portnum%"
if !errorlevel! neq 0 Echo %%a Not listening on port %portnum% >> report.txt

Report •

Related Solutions

March 12, 2011 at 04:24:09
That wouldn't do much if the Windows Firewall Service is started and enabled.

Happy is ONE who says I am an OttoMAN.

Report •

March 14, 2011 at 09:17:51

Thanks so much for your response. I think what you have provided code wise is what I am looking for (with minor tweak). Basically, I am using an automation tool to install an agent based app across 100s of servers . One of the pre-req this agent based app has is to make sure port # 2222 is not being utilized by anything other process or service.

So, my bat script should do a "check" for that port# and should tell me that given port is available.

I hope I am king sense here.

Thanks again for your help.

Report •

March 14, 2011 at 14:22:40
The batch-script of reply #3 from viddywell will hang for like 20-30 seconds if the target has Windows Firewall started and enabled.

So try to ping the target to be sure the host has Windows Firewall disabled, before you psexec(ute) some command(s).


ping -n 1 > nul
if errorlevel 1 ECHO Target does not respond. & pause & goto exit
ECHO Target is responsible ;)


Happy is ONE who says I am an OttoMAN.

Report •

March 15, 2011 at 10:08:12
Thanks for your replay MUs. Let me try.

Report •

Ask Question