我如何判断端口是开放还是关闭的?“开放端口”和“关闭端口”的确切含义是什么。
> netstat -n64l
...
tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN
...
udp 0 0 0.0.0.0:53 0.0.0.0:*
...
raw 0 0 0.0.0.0:1 0.0.0.0:* 7
...
更新:
从那时起,netstat
已经有些被弃用了,您可能想要学习 ss
:
ss -l4n
-- or --
ss -l6n
很遗憾,目前你必须为相应的堆栈(IPv4或IPv6)选择-4或-6。
如果您有兴趣编写C/C++代码或类似代码,可以从/proc/net/...
中读取这些信息。例如,TCP连接可以在此处找到:
/proc/net/tcp (IPv4)
/proc/net/tcp6 (IPv6)
int s = socket(
AF_NETLINK
, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK
, NETLINK_SOCK_DIAG);
/proc/...
文件更安全。我最喜欢使用的检查特定端口是否打开或关闭的工具是telnet
。在所有操作系统上都可以找到这个工具。
语法是:telnet <主机名/IP> <端口>
如果端口打开,它看起来像这样:
telnet localhost 3306
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
如果端口关闭,它看起来像这样:
telnet localhost 9999
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host
Port Scanner
以获取其他选项。