Linux系统如何判断端口是否打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9029297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:14:51  来源:igfitidea点击:

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

c++linuxfirewallports

提问by VamsiKrishna

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.

我需要找到是否有端口 Ex。端口 8080 在 Linux 系统上打开。我正在寻找一个 C 或 C++ API 调用,当给定端口号时,该调用将返回 true 或 false 或等效项。这只需要在本地运行。我不想知道是否有进程在侦听端口,只是没有被防火墙阻止。

I want to do the equivalent of

我想做相当于

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

采纳答案by 0x90

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

lsof -i

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

对于远程机器,尝试nmap甚至更简单、更基本的netcat. 例如

netcat targetHost 8080

netcat targetHost 8080