How to Make Portqry.exe Only Report Listening Ports (310513)



The information in this article applies to:

  • Microsoft Windows 2000 Server
  • Microsoft Windows 2000 Advanced Server
  • Microsoft Windows 2000 Professional

This article was previously published under Q310513

SUMMARY

This article describes how to make Portqry.exe report only ports that are listening and not report ports that are in other states.

Note Version 2 of Portqry.exe is now available. The Microsoft Download Center link at the end of this article has been updated to reflect the new version. Version 1.0 of Portqry.exe has been removed from the Microsoft Download Center.

MORE INFORMATION

You can use the Portqry.exe command-line tool to help troubleshoot TCP/IP connectivity issues. Portqry.exe runs on Windows 2000 and later Microsoft Windows operating systems. Portqry.exe reports the port status of target TCP and UDP ports on a computer you select. By default, Portqry.exe reports the status of a port as LISTENING, NOT LISTENING, or FILTERED. When you use the -q (quiet) switch with Portqry.exe, all output to the screen (except error messages) is suppressed. After the target port is queried, Portqry.exe returns one of the following values:
  • 0 (zero) is returned if the target port is listening.
  • 1 is returned if the target port is not listening.
  • 2 is returned if the target port is listening or filtered.
You can use this functionality in conjunction with batch files to customize the Portqry.exe output. For example, to obtain a list of only the ports that are listening on a remote computer, you could use the following two batch files.

Query.bat Defines What Ports to Query and Calls Listen.bat

:Top
@echo off

:loop
@echo PortQry is querying TCP ports 25,110,143,53, and 389
@echo It will only report which ports are listening

@FOR %%I IN (25 110 143 53 389) DO CALL listen.bat %%I

:end

Listen.bat Calls Portqry.exe and Passes it the Target Port

NOTE: Only the ports that are found listening are reported.

:Top
@echo off
@portqry -n myserver.reskit.com -e %1 -p tcp -q

@if errorlevel = 2 goto end
@if errorlevel = 1 goto end
@if errorlevel = 0 goto success

:success
@Echo Port %1 is listening
goto end

:end

Copy the text of each of the preceding batch files, paste this text into a text file, and then save these files as Query.bat and Listen.bat. Copy these files into the same folder that contains Portqry.exe, and then run Query.bat. Edit the list of ports in the Query.bat file, as well as the protocol that is used and the target server in the Listen.bat file to change the functionality as needed.

NOTE: When you use the -q switch with the -l (log file) switch, Portqry.exe overwrites an existing log file with the same name, and does not prompt you for permission to do so.

To Obtain Portqry.exe

Portqry.exe is available for download from the Microsoft Download Center. To download Portqry.exe, visit the following Microsoft Web site: For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

Modification Type:MajorLast Reviewed:12/18/2003
Keywords:kbhowto KB310513