How to ping and test for a specific port from Linux or Unix command line

kbd { display: inline-block; margin: 0 .1em; padding: .1em .6em; font-family: Arial,”Helvetica Neue”,Helvetica,sans-serif; font-size: 11px; line-height: 1.4; color: #242729; text-shadow: 0 1px 0 #FFF; background-color: #e1e3e5; border: 1px solid #adb3b9; border-radius: 3px; box-shadow: 0 1px 0 rgba(12,13,14,0.2), 0 0 0 2px #FFF inset; white-space: nowrap; }

I wanted to see if a specific port of a desktop computer or server is open from a Linux or Unix command line shell. How do I ping a specific port? Is it possible possible to ping an or ports?

You use the ping command to send ICMP ECHO_REQUEST packets to network computers, routers, switches and more. ping works with both IPv4 and IPv6. Ping is using ICMP protcol. The ping command can not be used to ping a specific port.

However, use any one of the following command to see if a port is open or not as follows:

Linux ping port using telnet command

The syntax is:
telnet {host} {port}
telnet 80
telnet 80

Sample outputs:

Connected to router.
Escape character is '^]'.
telnet> q
Connection closed.

To close your session, press Ctrl+]+q.

Use nc command

The syntax is:
nc -vz {host} {port}
nc -vz 80
nc -vz 443

Sample outputs:

found 0 associations
found 1 connections:
     1:	flags=82
	outif utun1
	src port 54997
	dst port 443
	rank info not available
	TCP aux info available

Connection to port 443 [tcp/https] succeeded!

Unix ping port using nmap command

The syntax is:
nmap -PNp {port} {host}
nmap -p {port} {host}
nmap -p 22
nmap -p 443

Sample outputs:

Starting Nmap 7.40 ( ) at 2017-05-24 01:00 IST
Nmap scan report for router (
Host is up (0.00034s latency).
443/tcp open  https

Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds

See “Top 32 Nmap Command Examples For Sys/Network Admins” for more info.

Use bash shell

The syntax is as follows:

## check for tcp port ##
## need bash shell ##
(echo >/dev/tcp/{host}/{port}) &>/dev/null && echo "open" || echo "close"
(echo >/dev/udp/{host}/{port}) &>/dev/null && echo "open" || echo "close"
(echo >/dev/tcp/ &>/dev/null && echo "Open 22" || echo "Close 22"
(echo >/dev/tcp/ &>/dev/null && echo "Open 443" || echo "Close 443"

## check for tcp port ## ## need bash shell ## (echo >/dev/tcp/{host}/{port}) &>/dev/null && echo "open" || echo "close" (echo >/dev/udp/{host}/{port}) &>/dev/null && echo "open" || echo "close" (echo >/dev/tcp/ &>/dev/null && echo "Open 22" || echo "Close 22" (echo >/dev/tcp/ &>/dev/null && echo "Open 443" || echo "Close 443"

Sample outputs:

Close 22
Open 443

Use nping command

Nping is an open-source tool for network packet generation, response analysis and response time measurement. Nping allows users to generate network packets of a wide range of protocols, letting them tunevirtually any field of the protocol headers. While Nping can be used as a simple ping utility to detect active hosts, it can also be used as a raw packet generator. The syntax is:
sudo nping --tcp -p {port} {host}
sudo nping --tcp -p 443

Sample outputs:

Fig.01: nping just pinged host at port 443

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...