This article is all about the different methods you can use to ping ports to help in troubleshooting network connectivity issues. The ping command is available across just about every device you can log in to, making it the go to tool to use to verify network connectivity to a remote host.
This no doubt looks familar..
$ ping 172.31.31.232
PING 172.31.31.232 (172.31.31.232) 56(84) bytes of data.
64 bytes from 172.31.31.232: icmp_seq=1 ttl=64 time=0.542 ms
64 bytes from 172.31.31.232: icmp_seq=2 ttl=64 time=0.187 ms
64 bytes from 172.31.31.232: icmp_seq=3 ttl=64 time=0.219 ms
Whilst it can be a useful test, the problem with this is that if pinging a remote host, for example across the internet, you may not get a response if the ICMP protocol (which Ping uses) is blocked along the way, or is blocked by the destination device. Instead, we could “ping” a specific port on the remote device.
If a server is listening on a particular port, for example port 80 for a web server, we can attempt to test connectivity to that port to establish whether it’s reachable. In the case of a web server, its job is to listen on port 80 (or indeed 443), so by making a connection (or pinging that port) we can verify if the service is available. There are a number of tools available to help us ping a specific port, depending on what operating system you are using.
Note– We’re not really pinging the port – the term ping technically refers to a ping request using the Internet Control Message Protocol (ICMP). What we are going to be doing here is making a connection on a given port – but you’ll often hear this phrased as ‘pinging the port’.
Let’s start by looking at NetCat, which is a tool often used on Linux, and can be found natively on many Linux distributions.
Ping Ports using NetCat
netcat (often abbreviated to nc) is a computer networking utility for reading from and writing to network connections using TCP or UDP. It is available on Linux, and is also is also available on ESXi hosts.
As we don’t want to interact with the server port, we just want to establish if it is open and listening, we use the -z
option with netcat, which is the scanning option. We also want to use -v
to turn on verbose output. With this in mind, lets ‘ping’ port 80 on a web server to see if it responds:
$ nc -vz 172.31.28.41 80
Connection to 172.31.28.41 80 port [tcp/http] succeeded!
Great! The output shows that nc has verified that it can communicate with the web server on port 80. We can ‘ping’ any port in this way. For example, to see if it is listening on port 22 (SSH):
$ nc -vz 172.31.28.41 22
Connection to 172.31.28.41 22 port [tcp/ssh] succeeded!
Again, we have a successful response to the connection request.
Ping a Range of Ports using NetCat
It’s also possible to use nc to scan a range of ports. To so use use:
# nc -vz 172.31.28.41 22-23
Connection to 172.31.28.41 22 port [tcp/ssh] succeeded!
nc: connect to 172.31.28.41 port 23 (tcp) failed: Connection refused
As you can see from the output, we have confirmed that port 22 is accessible, but port 23 is not.
NetCat has many, many more uses, but as a tool to do a quick check from a Linux system to see if a port is listening and is accessible, it is hard to beat! Next, lets take a look at how we can ping a port from a Windows device.
How to Use PowerShell to Ping a Port or Multiple Ports
PowerShell is available natively on modern Windows devices (and you can also install PowerShell on Linux now), so it is a great tool to use to test whether a remote server port is accessible, as it has a built-in cmdlet called test-netconnection
for pinging remote ports. Like with nc
, the only parameters you need to pass the cmdlet are the ip address and port that we want to ping on the remote host. For example:
As with NetCat
, you can also use test-netconnection
to ping a range of ports, using a simple PowerShell script. For example:
$target = 3.10.190.7
$ports = [22,23]
Foreach ($P in $Ports){
Test-NetConnection $target -Port $P -WarningAction SilentlyContinue
}
This is a simple for loop
which will ping each port listed in the ports variable against the IP in the target variable.
Final Thoughts
In this article you have learned how to ping ports on a remote host using native tools on both Linux and Windows systems. These techniques are useful to determine whether a server port is open and accessible on a remote host.
In the case of NetCat, this will also work from ESXi hosts which can be useful in troubleshooting vSphere connectivity issues.