Last Updated: February 25, 2016
· tmarwen

Check if a remote machine is listening on some specific port, in a *nix system

When working in a distributed environment, I always use to set up some boxes to communicate together using some transport layer (TCP or UDP). So there has been always a need to check whether this or that port is open on some machine, and since ping command is nothing but an Echo Request on some IP address, I had to look around for a simple but efficient solution and finally crossed one.

Here I share the script that does the checking using the nc (netcat tool), so you have to make sure it is already installed on your machine.

How To:

Once you are sure to have netcat ready for use, create a bash script with underlines code (either using your favorite text editor or following command line instructions which I will be using right now):

  • Create a bash script with name

  • Edit the script:

  • Then copy/paste below lines in:

    # portcheck tool
        echo -e "\n USAGE: ./${0##*/} [host|ip] [port] \n"
        local host=${1}
        local port=${2}
        if nc -w 5 -z ${host-ip} ${port} 2>/dev/null
        echo -e "\a\n => Port ${port} at ${host} is open"
        echo -e "\a\n => Port ${port} at ${host} is closed"
    [[ $# -ne 2 ]] && usage
    check_port ${1} ${2}
  • Make the script executable:

    chmod +x


The script is as straightforward as it can be and you can use it by just issuing the script name (in its directory path) followed by an IP address and a Port you want to check, e.g:

cd /path/to/
sh 80
 => Port 80 at is open

sh 8080
 => Port 8080 at is closed

Note: You can create a link to your script file under your /usr/bin directory so it will be available under your PATH env variable and you are free to use it as a command:

cd /usr/bin
sudo ln -s /path/to/ portcheck

Now you can just use the portcheck command from any path:

portcheck 80
 => Port 80 at is open

Source: virtual-server Blog

3 Responses
Add your response

I believe the usage function is missing port argument, and should be something like:

echo -e "\n USAGE: ./${0##*/} [host|ip] [port]\n"
over 1 year ago ·

Good catch @irakli, I will add the missing argument.

over 1 year ago ·

Sort of heavy weaponry: check out nmap.

over 1 year ago ·