Netstat在python中。有可能吗? [英] Netstat in python. Does it's possible?
问题描述
大家好。我不知道Python是否适合这类工作,但是我想知道它是否有可能模仿netstat。 Python中的命令。
我需要知道某个可执行文件是否打开了套接字,在这种情况下,
我想知道是什么类型的它使用的套接字(TCP或UDP),它的src / dst PORT,
和连接的当前状态(监听,建立,SYN
发送...)。
提前致谢。
billie启发我们:大家好。我不知道Python是否适合这种工作,但我想知道它是否可以模仿netstat。 Python中的命令。
在Linux上,你可以阅读/ proc获取该信息,iirc。
Sybren >
-
世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉
安全标签来解决问题呢? br />
Frank Zappa
billie写道:大家好。我不知道Python是否适合这种工作,但我想知道它是否可以模仿netstat。 Python中的命令。
作为一般建议,使用strace(1)来回答这种
问题。运行strace -o tmp netstat,然后检查tmp,找出
netstat如何获得它报告的信息。
正如Sybren建议的,这可以都可以从/ proc回答。对于您感兴趣的
流程,列出/ proc /< pid> / fd(使用os.listdir),
然后读取所有链接的内容(使用os.readlink)。如果链接
值以[socket:开头,则它是一个套接字。然后搜索
/ proc / net / tcp作为ID。包含ID的行将有
您想要的信息。
问候,
Martin
2006年2月11日星期六16:28:06 +0100,Martin v.L?wis< ma **** @ v.loewis.de>写道:billie写道:大家好。我不知道Python是否适合这种工作,但我想知道它是否可以模仿netstat。 Python中的命令。
作为一般建议,使用strace(1)来回答这种问题。运行strace -o tmp netstat,然后检查tmp,找出netstat如何获得它报告的信息。
好主意。
人们做这样的事情可能仍然存在问题:netstat
可能会使用不稳定或非公共API来查找它列出的内容。这是好的b $ b,因为它通常是你的操作系统供应商必须处理它(当/ proc或/ sys文件系统布局发生变化时,运送另一个netstat等等) )。
如果它的工作原理如此,你可以很好地从Python访问API - 但是你没有写一个便携式的pynetstat很多努力和维护。
/ Jorgen
-
// Jorgen Grahn< grahn @ Ph''nglui mglw''nafh Cthulhu
\ X / snipabacken.dyndns.org> R''lyeh wgah''nagl fhtagn!
Hi all. I don''t know if Python is good for this kind of jobs but I''m
wondering if it''s possible emulate the "netstat" command in Python.
I''d need to know if a certain executable opened a socket and, in that case,
I''d like to know what kind of socket it uses (TCP or UDP), its src/dst PORT,
and the current STATE of the connection (listening, established, SYN
sent...).
Thanks in advance.
billie enlightened us with:Hi all. I don''t know if Python is good for this kind of jobs but I''m
wondering if it''s possible emulate the "netstat" command in Python.
On Linux, you can read /proc for that info, iirc.
Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
billie wrote:Hi all. I don''t know if Python is good for this kind of jobs but I''m
wondering if it''s possible emulate the "netstat" command in Python.
As a general recommendation, use strace(1) to answer this kind of
question. Run "strace -o tmp netstat", then inspect tmp to find out
how netstat obtained the information it reported.
As Sybren suggests, this can all be answered from /proc. For a
process you are interested in, list /proc/<pid>/fd (using os.listdir),
then read the contents of all links (using os.readlink). If the link
value starts with "[socket:", it''s a socket. Then search
/proc/net/tcp for the ID. The line containing the ID will have
the information you want.
Regards,
Martin
On Sat, 11 Feb 2006 16:28:06 +0100, Martin v. L?wis <ma****@v.loewis.de> wrote:billie wrote:Hi all. I don''t know if Python is good for this kind of jobs but I''m
wondering if it''s possible emulate the "netstat" command in Python.
As a general recommendation, use strace(1) to answer this kind of
question. Run "strace -o tmp netstat", then inspect tmp to find out
how netstat obtained the information it reported.
Good idea.
There might still be a problem for people doing things like this: netstat
might use unstable or non-public APIs to find the things it lists. This is
fine because it''s typically your OS vendor who have to handle that (ship
another netstat when the /proc or /sys file system layout changes, etc).
If it works like that, you can access the APIs fine from Python -- but you
cannot write a portable ''pynetstat'' without a lot of effort and maintenance.
/Jorgen
--
// Jorgen Grahn <grahn@ Ph''nglui mglw''nafh Cthulhu
\X/ snipabacken.dyndns.org> R''lyeh wgah''nagl fhtagn!
这篇关于Netstat在python中。有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!