如何在 Python/Windows 中列出网络接口、其配置 IP、网络掩码和网关
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4496441/
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
How to list network interfaces, its configuration IP,netmask and gateway in Python/Windows
提问by ntcong
I wanna develop a small application to monitor arp and scan arp list in local network.
我想开发一个小应用程序来监视本地网络中的 arp 和扫描 arp 列表。
Currently, I need to retrieve the list of network interfaces and its configuration.
目前,我需要检索网络接口列表及其配置。
Most of the time I work with Linux, so I don't know much about Win32 API, is there anyway to do this in python way ? I'm using Python 2.6 with PyQt and Scapy (has pywin32 aswell, so if you provide the detail way with win32, I will try)
我大部分时间都在使用 Linux,所以我对 Win32 API 不太了解,无论如何可以用 python 的方式来做到这一点吗?我将 Python 2.6 与 PyQt 和 Scapy 一起使用(也有 pywin32,所以如果您提供有关 win32 的详细信息,我会尝试)
I found pcapy.findalldevs(), but it cannot retrive configuration. I don't care much about the name, just configuration(IP,netmask) is OK.
我找到了 pcapy.findalldevs(),但它无法检索配置。我不太关心名称,只是配置(IP,网络掩码)就可以了。
采纳答案by Rafe Kettler
Not sure exactly what info you want, but ipconfig /all
certainly sounds like what you want:
不确定您想要什么信息,但ipconfig /all
听起来肯定是您想要的:
import subprocess
subprocess.call('ipconfig /all')
回答by Andre Miras
For Linux, I use pynetlinux
对于 Linux,我使用pynetlinux
pip install pynetlinux==1.1
>>> from pynetlinux import ifconfig >>> ifconfig.Interface("eth0").get_ip() '192.168.1.66'
Edit: This is indeed for Linux, for a cross-platform solution, see my other answer.
编辑:这确实适用于 Linux,对于跨平台解决方案,请参阅我的其他答案。