?
Solved

Command Line Utility To Check For Open Ports (SOLVED)

Posted on 2007-07-27
9
Medium Priority
?
23,027 Views
Last Modified: 2009-07-07
I need a command line tool to check if a port is open on my server. It should no need any 3rd party packages such as winCap ..I simply need it to incorporate it into a batch file.
I have checked out nmap but it needs a third party package. I want to make the batch file easily distributable.
Thanks
0
Comment
Question by:http:// thevpn.guru
  • 5
  • 4
9 Comments
 
LVL 19

Author Comment

by:http:// thevpn.guru
ID: 19581519
http://www.petri.co.il/quickly_find_remote_open_ports.htm

C:\WINDOWS>portqry -n www.petri.co.il -p tcp -e 80

Querying target system called:

www.petri.co.il

Attempting to resolve name to IP address...

Name resolved to 64.5.48.48

TCP port 80 (http service): LISTENING
0
 
LVL 14

Expert Comment

by:canali
ID: 19586294
@echo off
::
::  chkOpenPort.cmd
::  only batch
::       gastone canali
setlocal
if +%1+==++ goto :_err nohost
if +%2+==++ goto :_err
set host=%1
set port=%2
set result=Port:%port% on %host% closed!
start /min telnet.exe %host% %port%
for /f "tokens=5 delims= " %%p in ('netstat -ao^|find /i "ESTABLISHED"^| find /i "%host%:%port%"') do (
   rem echo Port:%port% on %host% OPEN!
   set result=Port:%port% on %host% OPEN!
   ping -n 2 localhost >nul 2>&1
   tskill %%p >nul 2>&1
)
echo %result%
goto :_END
:_err
echo error: Not enought parameters
echo syntax: chkOpenPort.cmd hostname port

:_END
0
 
LVL 19

Author Comment

by:http:// thevpn.guru
ID: 19590648
I did not know that netstat can check for open ports on remote servers are you sure about that ?
I did use portqry which was designed for that purpose
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 14

Expert Comment

by:canali
ID: 19591092
Netstat don't check open ports! The idea behind the script is open a telnet connection to the remote host remotePort (is the fast way to test if a TCP port is open ) if connection is succesful give you an "ESTABLISHED" line (the port is open) and then I kill the telnet process.

My solution is for check open ports only with standard OS installation without the need of other tool...  is another way, is not the way and is not a portqry substitute...
0
 
LVL 19

Author Comment

by:http:// thevpn.guru
ID: 19591108
I would not want to give the users telnet access in any kind of way to my servers..that would be to much of a security risk..telnet is only open on the localhost for testing purposes.
0
 
LVL 14

Expert Comment

by:canali
ID: 19591130
I speak about telnet.exe (client) ...
You don't need Telnet server!!!!

0
 
LVL 19

Author Comment

by:http:// thevpn.guru
ID: 19591204
I got you...but still it cant check for upd and tcp ports...what if I wana check for UDP 53 or TCP 53..

And it gave the following result on www.google.com 

C:\Documents and Settings\ali.jawad\Desktop>poing.bat www.google.com 80
Port:80 on www.google.com closed!
The system cannot find the batch label specified - _END
0
 
LVL 14

Expert Comment

by:canali
ID: 19604153
@echo off
::
::  chkOpenPort.cmd
::  only batch
::       gastone canali
:: v.0.1
setlocal
if +%1+==++ goto :_err nohost
if +%2+==++ goto :_err
set host=%1
set port=%2
for /f "tokens=2* delims=[" %%h in ('ping -n 1 %host%^|find /i "["') do for /f "tokens=1 delims=]" %%H in ('echo %%h') do set hostIP=%%H
if +%hostIP%+==++ goto :_IPNOTFOUND
set result=Port:%port% on %host% closed!
start /min telnet.exe %hostIP% %port%
ping -n 2 localhost >nul 2>&1
for /f "tokens=5 delims= " %%p in ('netstat -ano^|find /i "ESTABLISHED"^| find /i "%hostIP%:%port%"') do (
   rem echo Port:%port% on %host% OPEN!
   set result=Port:%port% on %host% OPEN!
   tskill %%p >nul 2>&1
)
echo %result%
goto :_END
:_err
echo error: Not enought parameters
echo syntax: chkOpenPort.cmd hostname port
goto :_END
:_IPNOTFOUND
echo %host%: host unknown
:_END
0
 
LVL 14

Accepted Solution

by:
canali earned 1500 total points
ID: 19613044
::
::testport.cmd
:: Test port using portqry.exe
::
@echo off
setlocal
::add path to PortQryV2
set path="C:\Documents and Settings\bill.gates\Desktop\PortQryV2(1)\PORTQR~1\";%path%
:: set host
set host=www.google.com
:: set dns port to test
set port=53
::check tcp port
portqry -n %1 -p tcp -e %port% -q
call :_%errorlevel% TCP
::check UDP port
portqry -n %1 -p udp -e %port% -q
call :_%errorlevel% UDP
goto :_END

:_0
  echo port %port% protocol %1 is open on %host%
goto :_EOF
:_1
  echo port %port% protocol %1  is closed on %host%
goto :_EOF
:_2
  echo port %port% protocol %1 is closed or filtered on %host%
goto :_EOF
:_99
echo syntax error: portqry -n %1 -p both -e %port% -q -i

:_END
:_EOF
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data center, now-a-days, is referred as the home of all the advanced technologies. In-fact, most of the businesses are now establishing their entire organizational structure around the IT capabilities.
In this article I will be showing you how to subnet the easiest way possible for IPv4 (Internet Protocol version 4). This article does not cover IPv6. Keep in mind that subnetting requires lots of practice and time.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question