809

Just a quick sanity check here.

Can you ping a specific port of a machine, and if so, can you provide an example?

I'm looking for something like ping ip address portNum.

voretaq7
  • 79,879
  • 17
  • 130
  • 214
Davie
  • 8,215
  • 3
  • 12
  • 8

18 Answers18

839

You can't ping ports, as Ping is using ICMP which is an internet layer protocol that doesn't have ports. Ports belong to the transport layer protocols like TCP and UDP.

However, you could use nmap to see whether ports are open or not

nmap -p 80 example.com

Edit: As flokra mentioned, nmap is more than just a ping-for-ports-thingy. It's the security auditers and hackers best friend and comes with tons of cool options. Check the doc for all possible flags.

sfussenegger
  • 8,602
  • 1
  • 16
  • 7
  • 18
    You might want to add `-PN` to skip the host discovery nmap usually does before testing the given port. – flokra Oct 08 '09 at 10:06
  • 31
    To save some people some trouble: nmap /can/ be installed on windows from [link](http://nmap.org/book/inst-windows.html) (or google `nmap windows`), BUT it does not work over VPNs. PaPing didn't seem to be able to scan a range of addresses. I have posted Python script below that will work over VPN to scan a range of ports on a range of addresses. –  Sep 08 '11 at 07:19
  • 24
    For others: nmap doesn't exist on Mac OS X, you can install nmap via homebrew `brew install nmap` or MacPorts. – Highway of Life Mar 27 '12 at 15:58
  • 21
    nping is better suited for this task than nmap. nping is part of the nmap distribution. – CMCDragonkai Jun 16 '14 at 08:39
  • 8
    Mac OSX has 'Network Utility' which has a port scanner tab – Shanimal Jul 27 '15 at 20:53
  • 2
    Is there a way to use the Network Utility in shell? – Gerard Apr 05 '16 at 07:43
  • i did nmap -p 80 , it says host is down , please help, how to open 80 port to internet – user1735921 Aug 15 '16 at 13:23
  • If it is to use as a test, you can also try (if your version of `nc` as the option) : `nc -z host port` (default is tcp, but you can specify udp). The return code can be used in a test. – Esteban May 12 '17 at 06:44
222

Open a telnet session to the specific port, for example:

# telnet google.com 80
Trying 74.125.226.48...
Connected to google.com.
Escape character is '^]'.

To close your session, hit Ctrl+].

Ajay
  • 137
  • 1
  • 8
157

If you're on a windows installation with powershell v4 or newer, you can use the test-netconnection powershell module:

Test-NetConnection <host> -port <port>

Example: Test-NetConnection example.com -port 80

This cmdlet also has the alias tnc. Eg tnc example.com -port 80

Ashley
  • 648
  • 1
  • 6
  • 15
Kjetil Limkjær
  • 2,023
  • 2
  • 16
  • 16
123
$ nc -vz google.com 80
Connection to google.com 80 port [tcp/http] succeeded!
Rahul Patil
  • 2,941
  • 3
  • 14
  • 10
85

You can use PaPing:

http://code.google.com/p/paping

C:\>paping.exe www.google.com -p 80 -c 4
paping v1.5.1 - Copyright (c) 2010 Mike Lovell

Connecting to www.l.google.com [209.85.225.147] on TCP 80:

Connected to 209.85.225.147: time=24.00ms protocol=TCP port=80
Connected to 209.85.225.147: time=25.00ms protocol=TCP port=80
Connected to 209.85.225.147: time=24.00ms protocol=TCP port=80
Connected to 209.85.225.147: time=24.00ms protocol=TCP port=80

Connection statistics:
        Attempted = 4, Connected = 4, Failed = 0 (0.00%)
Approximate connection times:
        Minimum = 24.00ms, Maximum = 25.00ms, Average = 24.25ms
38

Try curl command, like:

$ curl host:port

For example:

$ curl -s localhost:80 >/dev/null && echo Success. || echo Fail.
Success.

Above command will return Fail on a non-zero exit status codes. In some particular cases, such as empty or malformed response (see man curl), you may want to handle specific exit codes as successful, so please check this post for more detailed explanation.

kenorb
  • 6,499
  • 2
  • 46
  • 54
  • 5
    I like this because `curl` is present by default on CentOS, so I don't have to install anything. – bdemarest Jan 12 '16 at 17:34
  • curl worked on a Raspberry Pi (distro Raspian) – John M Jul 25 '16 at 17:41
  • 1
    This is by far the most elegant and universal solution. I tweaked it slightly to make it a neat oneliner: `IP="" ; PORT="" ; curl -s "$IP:$PORT" > /dev/null && echo "Success connecting to $IP on port $PORT." || echo "Failed to connect to $IP on port $PORT."` – Lefty G Balogh Apr 28 '17 at 14:36
30

No, you can't, because ping uses the ICMP protocol, which doesn't even have a concept of ports.

Preview
  • 105
  • 4
27

I found a simpler solution using PsPing:

psping 192.168.2.2:5000

It's part of Windows Sysinternals.

PsPing implements Ping functionality, TCP ping, latency and bandwidth measurement.

kenorb
  • 6,499
  • 2
  • 46
  • 54
Ayan Mullick
  • 486
  • 1
  • 6
  • 14
  • 3
    Also PsPing comes from Microsoft SysInternal team so is might be considered a little more legitimate when dealing with other Microsoft oriented people. (And has better functionality than the very old PortQry) – martyvis Apr 30 '18 at 00:08
  • 2
    PSPing has the advantage of providing latency measurement when using a custom port, something that Test-NetConnection cannot do (it only reports the ICMP response time). – Jeff Miles Jan 04 '19 at 19:30
18

On Linux you can use hping but it uses TCP, rather than ICMP.

hping example.com -S -V -p 80
Ladadadada
  • 26,337
  • 7
  • 59
  • 90
c4f4t0r
  • 5,301
  • 3
  • 31
  • 42
  • 1
    hping (hping3 in my ubuntu) is excellent. It periodically repeats the probe like ping does, and unlike many of the tools suggested here. It also test both whether the packet gets through, and whether the port is open. If it's open you get `flags=SA` (i.e. SYN ACK), and if it's closed you get `flags=SR` (i.e. SYN RST). Note that you probably don't need the `-V` flag here, but you do need sudo/root to run hping. – mc0e Feb 07 '17 at 04:19
  • Excellent! Unlike many other answers, this reports RTT latency like `ping` and pings until stopped. – Edward Anderson Jun 04 '18 at 19:46
11

Ping is very specific but if you want to check whether a port is open or not, and are running a Windows box then PortQry is your friend.

I've only used it for testing Domain Controllers for connectivity issues, but it worked a treat for that, so should work for you.

Rob
  • 647
  • 1
  • 7
  • 27
  • 1
    PortQry was originally released in 1999/2000 by the MS Exchange team but MS recent version appears to have stunted it by removing the ability to query remote ports (health & safety). – Luke Puplett Jul 09 '12 at 16:31
8

Here's a quick and dirty .NET console app:

    static void Main(string[] args)
    {
        string addressArgument = null, portArgument = null;

        System.Net.Sockets.TcpClient tcpClient = null;

        try
        {
            addressArgument = args[0];
            portArgument = args[1];

            int portNumber;
            portNumber = Int32.Parse(portArgument);

            tcpClient = new System.Net.Sockets.TcpClient();
            tcpClient.ReceiveTimeout = tcpClient.SendTimeout = 2000;


            IPAddress address;
            if (IPAddress.TryParse(args[0], out address))
            {
                var endPoint = new System.Net.IPEndPoint(address, portNumber);
                tcpClient.Connect(endPoint);
            }
            else
            {
                tcpClient.Connect(addressArgument, portNumber);
            }

            Console.WriteLine("Port {0} is listening.", portArgument);
        }
        catch (Exception e)
        {
            if (e is SocketException || e is TimeoutException)
            {
                Console.WriteLine("Not listening on port {0}.", portArgument);
            }
            else
            {
                Console.WriteLine("Usage:");
                Console.WriteLine("    portquery [host|ip] [port]");
            }
        }
        finally
        {
            if (tcpClient != null)
                tcpClient.Close();
        }
    }
Luke Puplett
  • 939
  • 3
  • 16
  • 24
5

No.

There's no guarantee that the service running on the port understands ping. It also opens up the question of what "flavor" of port you want to ping, TCP or UDP? Since the ping "protocol" uses neither (ping is implemented using ICMP), it doesn't make a lot of sense.

unwind
  • 344
  • 2
  • 4
5

This is the only solution that works for VPNs with the client machine being Windows Vista or Windows 7, as other listed answers simply do not function. This answer was previously deleted and should not have been, as this is the only solution for a real-world common case. Since there is no appeal available for the delete, I am reposting it to save others the frustration I had with trying to use the other answers.

The example below finds which IPs on the VPN that have VNC/port 5900 open with the client running on Windows 7.

A short Python (v2.6.6) script to scan a given list of IPs and Ports:

from socket import *

fTimeOutSec = 5.0
sNetworkAddress = '192.168.1'
aiHostAddresses = range(1,255)
aiPorts = [5900]

setdefaulttimeout(fTimeOutSec)
print "Starting Scan..."
for h in aiHostAddresses:
    for p in aiPorts:
        s = socket(AF_INET, SOCK_STREAM)
        address = ('%s.%d' % (sNetworkAddress, h))
        result = s.connect_ex((address,p))
        if ( 0 == result ):
            print "%s:%d - OPEN" % (address,p)
        elif ( 10035 == result ):
            #do nothing, was a timeout, probably host doesn't exist
            pass
        else:
            print "%s:%d - closed (%d)" % (address,p,result)

        s.close()
print "Scan Completed."

Results looked like:

Starting Scan...
192.168.1.1:5900 - closed (10061)
192.168.1.7:5900 - closed (10061)
192.168.1.170:5900 - OPEN
192.168.1.170:5900 - closed (10061)
Scan Completed.

The four variables at the top would need to be changed to be appropriate to whatever timeout, network, hosts, and ports that are needed. 5.0 seconds on my VPN seemed to be enough to work properly consistently, less didn't (always) give accurate results. On my local network, 0.5 was more than enough.

Mark
  • 168
  • 1
  • 5
  • 2
    Works perfectly on Win 7 XP Pro client / Win Server 2008 R2 to diagnose closed port. (I had to use a 30 second timeout threshold but that may be an issue with specific server environment not a problem with this code) – David Zemens Sep 04 '15 at 02:04
4

As per CMCDragonkai's comment, you can use nping, which is part of Nmap.

nping example.com --tcp-connect -p 80,443

Here's a link to the manpage.

mwfearnley
  • 816
  • 1
  • 11
  • 22
4

I'm quite sure that Nagios check_tcp probe does what you want. They can be found here and although designed to be used in a Nagios context, they're all standalone programs.

$ ./check_tcp -H host -p 22
TCP OK - 0.010 second response time on port 22|time=0.009946s;0.000000;0.000000;0.000000;10.000000
fvu
  • 686
  • 3
  • 7
3

In Bash shell, you can use TCP pseudo-device file, for example:

</dev/tcp/serverfault.com/80 && echo Port open || echo Port closed

Here is the version implementing a timeout of 1 second:

timeout 1 bash -c "</dev/tcp/serverfault.com/81" && echo Port open || echo Port closed
kenorb
  • 6,499
  • 2
  • 46
  • 54
2

There is a lightweigth tool for it, called tcping: http://www.linuxco.de/tcping/tcping.html

Craig
  • 570
  • 4
  • 13
0

If you are running a *nix operating system try installing and using "zenmap", it is a GUI for nmap and has several useful scan profiles which are a great help to the new user.