Search code examples
c++linuxfirewallports

How to know if a port is open or not in Linux system


I need to find whether a port Ex. port 8080 is opened on a Linux system. I'm looking for a C or C++ API call that will return true or false or the equivalent when given a port number. This only needs to run locally. I don't want to know if there is a process listening on the port, only that is not blocked by the firewall.

I want to do the equivalent of

 if( ! IsPortOpen(8080))
     cout << "please request IT to open port 8080 before running this tool";
     exit(1);
 }

Solution

  • I assume you are looking for open ports on the local machine. There are a lot of tools to do this.

    One of the easier to use and understand is

    lsof -i

    For remote machines, try nmapor even easier and more basic netcat. For example

    netcat targetHost 8080