如何获取端口关联到打开它们的应用程序? [英] How I can get ports associated to the application that opened them?

查看:150
本文介绍了如何获取端口关联到打开它们的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取我机器上所有打开的端口的列表,以及打开它们的应用程序。
我需要以编程方式获取此信息。



谢谢。

解决方案

我希望一个更聪明的答案会出现。我只是这样(在Python中以编程方式),试图重写一个名为NetHogs的程序。我的在这里,特别是这里是Python中的模块用于解析表从 / proc 。如果您不是 Python 识字(去学习),请查看原始的NetHogs ,它使用了C / C ++的混合(并且有点痛苦阅读,因此重写)。

$ b $值得注意的是,从 / proc 解析套接字信息的大量或快速重复的尝试是非常耗费CPU的,因为操作系统必须处理每个系统调用,动态解析内部结构。因此,你会发现一些缓存,以及我链接到你的两个项目的源的时间假设。



缺点是,你需要将 / proc /< pid> / fd 中为每个进程指定的套接字inode与 / proc / net /< proto> ; 。同样,示例解析,以及如何定位所有这些都存在于两个项目中。


I need to get a list of all opened ports on my machine and what application opened them. I need to get this information programmatically.

Thanks.

解决方案

I was hoping a cleverer answer would appear. I did just this (programmatically in Python), in an attempt to rewrite a program called NetHogs. My version is here, specifically here is the module in Python used to parse the table from /proc. If you're not Python literate (go learn it), then take a look at the original NetHogs, which uses a blend of C/C++ (and is a bit painful to read hence the rewrite).

It's worth noting that extensive or quickly repeated attempts to parse socket information from /proc is very CPU intensive, as the operating system has to handle every syscall made, and parse internal structures dynamically. As such you'll find some caching, and timing assumptions made in the source of both projects I've linked you to.

The short of it is, you need to relate the socket inodes given for each process in /proc/<pid>/fd to the connections given in /proc/net/<proto>. Again, example parsing, and how to locate all of these are present in both projects.

这篇关于如何获取端口关联到打开它们的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆