Few tricks for DevOps operations in Linux
During networking troubleshooting, to check if a port on the remote server is open, we can use the following command to test telnet via port:
$ telnet <hostname> 6663
Trying xxx.xx.xx.xx…
Connected to .
Escape character is '^]'.
^CConnection closed by foreign host
But under some circumstances telnet is not installed on the server or we can’t use telnet (for internal reasons), here are the alternatives to check ports, as telnet:
ncat
ncat (or nc) is a versatile and powerful command-line utility in Linux that is used for reading from and writing to network connections using TCP or UDP. It is a part of the Nmap Project and is an improved version of the traditional netcat command. ncat is often referred to as the "Swiss Army knife" of networking tools due to its flexibility and numerous use cases.
It is designed to be a reliable back-end tool to instantly provide network connectivity to other applications and users. Ncat will not only work with IPv4 and IPv6 but provides the user with a virtually limitless number of potential uses.
$ nc -zv <hostname> 443
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to xxx.xxx.xxx.xxx:443.
Ncat: 0 bytes sent, 0 bytes received in 0.02 seconds.
nmap
nmap (Network Mapper) is a powerful and widely-used open-source tool for network scanning, security auditing, and network discovery. It is designed to efficiently discover hosts and services on a computer network, creating a "map" of the network. nmap is a versatile tool and is commonly used by network administrators, security professionals, and penetration testers.
However in most organizations nmap is considered as a scaning tool and one can not use nmap without proper authorization, so use it with caution.
$ nmap -sT google.com
Starting Nmap 7.70 ( https://nmap.org ) at 2022-11-04 21:01 GMT
Nmap scan report for google.com (172.217.23.14)
Host is up (0.12s latency).
rDNS record for 172.217.23.14: lhr35s01-in-f14.1e100.net
Not shown: 998 filtered ports
PORT STATE SERVICE
80/tcp open http
443/tcp open https
curl
curl is a tool for transferring data from or to a server. It supports these protocols: DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, TFTP, WS and WSS.
$ curl -vvv telnet://<hostname>:443
* About to connect() to <hostname> port 443 (#0)
* Trying xxx.xxx.xxx.xxx...
* Connected to <hostname> (xxx.xxx.xxx.xxx) port 443 (#0)
bash shell
If using Bash Shell, then you can use its feature to check if a port is open or closed:
$ (timeout 1 bash -c '</dev/tcp/<hostname>/443' && echo PORT OPEN || echo PORT CLOSED) 2>/dev/null
PORT OPEN
BASH often provides the support that we need using redirection from /dev/proto/host/port. The /dev directory contains entries for the physical devices that may or may not be present in the hardware. When executing a command on a /dev/tcp/$host/$port pseudo-device file, Bash opens a TCP connection to the associated socket.
python
You can use Python socket module to simply check if a port is open or not:
import socket
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(4)
result = sock.connect(('<hostname>', 443))
print("Port is open")
except socket.error as e:
print("Port is not open")
finally:
sock.close()


