9

How do I check if a port is consistently alive? For example, I could use

ping 192.168.1.1 -t > results.txt

This will ping 192.168.1.1 continuously so that I could monitor it.
Is there an equivalent tool or command that I could use for this?

Currently I use telnet but sometimes the host disconnects it. I need a Windows solution.

3
  • Is it a specific port you need to check e.g. port 80 for http or port 21 for ftp, or is it a specific IP address you need to check i.e. to test if the host is reachable. The latter is what your above example is doing.
    – Chris
    Feb 8 '11 at 13:16
  • i'm wanting to check a specific port if it is open.
    – grassbl8d
    Feb 8 '11 at 13:29
  • Since "standard" ping uses ICMP messages, and when talking about ports you probably think about TCP/IP, you have a mismatch there that you can not close (ICMP and IP are very different protocols). ping is the wrong tool for monitoring, you should use something that generates the kind of traffic you need for the specific service. Example: to monitor a website you send an HTTP query, etc. May 16 '18 at 22:52
13

You could use netcat if there is a Windows version - on Linux I use:

nc -z <host> <port>

This returns 0 if the port is open. Run this in a loop for make it continuous.

If Powershell is available, see https://web.archive.org/web/20111102182913/http://poshcode.org/85 for an example.

3
  • 2
    ping every second a host on a port and return 0 if successfully so, 1 else: while (:); do nc -z HOSTNAME_HERE PORT_HERE; echo $?; sleep 1; done
    – sjas
    Jun 8 '15 at 14:59
  • link doesnt work anymore Aug 11 '19 at 11:32
  • @ClintonWard Updated with an archived link :)
    – Andrew
    Aug 12 '19 at 12:28
5

Or use nmap from http://nmap.org , there is a windows version available.

nmap -p port host

or, for hosts not responding to ICMP requests,

nmap -P0 -p port host

5

You could use nping from nmap like:

C:\>nping --tcp -p 80 192.168.1.1

where -p specifies the port to scan (here: 80). Furthermore you can use -H for hiding sent packets, in favor of showing only replies.

0

telnet <host> <port> will check remote <host> for a TCP listener on <port>.

0

If you want to go with stock command line utils, then TELNET will connect to a port for you. On the flip side, if there is service responding, then TELNET may 'hang' while waiting for you to supply the escape command sequence.

If you're open to installing new command line utils, then PaPing works as you want. It's cross platform, and there is a Windows installer available.

0

Edited: Download nmap command line tools from nmap.org, there you will find ncat. Copy and paste this text and to text editor and save as .BAT-file

@Echo Off
REM *************************************************************************
REM ** Port tester for Service Uptime Logging using NCAT.EXE from nmap.org **
REM **                                                                     **
REM ** Static variables:                                                   **
REM ** Host_IP = Hostname or IP you want to test                           **
REM ** Host_Port = Port number service are listening to                    **
REM ** LogFileName = Name of Logfile                                       **
REM ** WaitTime = Ca. number of seconds between each query                 **
REM *************************************************************************

:Pre
SET Host_IP=127.15.4.75
SET Host_Port=80
SET LogFileName=Ncat_%Host_IP%_%Host_Port%.txt
SET WaitTime=10

:Start
ping 127.15.4.75 -w 1000 -n %WaitTime% > NUL
ncat -z %Host_IP% %Host_Port% >NUL
if errorlevel=2 Goto Error2
if errorlevel=1 Goto Error1
if errorlevel=0 Goto Error0
Goto Start

:Error2
Echo %time% Other Error >> %LogFileName%
Goto Start

:Error1
Echo %time% Network Error >> %LogFileName%
Goto Start

:Error0
Echo %time% Connection successful >> %LogFileName%
Goto Start
:End

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.