问题描述
如何用C检查本地计算机上的端口(如果也需要通过传递IP或接口来实现)是否处于侦听状态?我不想连接到该端口进行检查,因为我不想激怒该端口后面的服务.
How do I check with C if a port on my local machine (if required by passing an IP or interface, too), is in listen state? I don't want to connect to this port for checking because I don't want to irritate the service behind this port.
我想用它来将缺少的net.tcp.listen项目添加到Zabbix.
I want to use this to add the missing net.tcp.listen item to Zabbix.
编辑-这是真正的答案:
EDIT - THIS IS THE REAL ANSWER:
正确的方法是读取套接字表:
The correct way is to read the socket tables:
/proc/net/tcp /proc/net/tcp6
/proc/net/tcp /proc/net/tcp6
它们包含如下行:
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
0: 00000000:1F40 00000000:0000 0A 00000000:00000000 00:00000000 00000000 101 0 4083927 1 f5d15240 750 0 0 2 -1
1: 00000000:2742 00000000:0000 0A 00000000:00000000 00:00000000 00000000 1002 0 6100 1 decd76c0 750 0 0 2 -1
,并且可以轻松地解析侦听套接字(dst:00000000:0000). netstat上的strace显示netstat的工作方式相同.
and can easily parsed for listening sockets (dst:00000000:0000). An strace on netstat shows that netstat works the same way.
推荐答案
类似于netstat
吗?
`netstat -anp`
将列出所有端口,并且应该解决这个问题.
Will list all the ports and should do the trick.
由于这是开源软件,因此您可能会从中获得一些启发.而且我相信它是用C编写的.
Since this is open-source software, you can probably take some inspiration in it. And i believe it is written in C.
这篇关于在Linux上,如何在不尝试连接的情况下检查端口是否处于侦听状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!