Checking ports using Bash

Ping

When we want to check whether a host is reachable (at all), we can use the ping command:

ping www.roland-kluge.de

It sends ICMP Echo Requests to the given host and reports how many packages are received and responded to. The option -c can be used to set how many requests are sent.

Checking specific endpoints

A disadvantage of ping is that we cannot configure a specific endpoint (= host + port) to ping. More advanced tools offer alternatives. The following command pings the FTP port of www.roland-kluge.de:

nping -p 21 www.roland-kluge.de

nping belongs to the nmap package. On Ubuntu, nmap can be installed as follows: sudo apt-get install nmap .

Another tool is  Netcat. The advantage of nc is that the error code indicates whether the endpoint was pinged successfully, making it useful for checking endpoint availability in scripts.

nc -w 1 www.roland-kluge.de 21
echo $? # returns 0
nc -w 1 www.roland-kluge.de 1
echo $? # returns 1

On Ubuntu, Netcat can be installed as follows: sudo apt-get install netcat .

References

  • [1] Nmap project site
  • [2] Netcat project site

Leave a Reply