【发布时间】:2016-10-17 22:23:09
【问题描述】:
是否可以编写一个小脚本来发送 DHCP 广播请求并找到 DHCP 服务器地址?
我在一个项目中需要这个,但我的研究让我相信你不能在 Windows 上做到这一点?我需要一个用于 OSX、Linux 和 Windows 的小脚本。
【问题讨论】:
-
为什么不能运行
ipconfig,例如,提取网关信息?
是否可以编写一个小脚本来发送 DHCP 广播请求并找到 DHCP 服务器地址?
我在一个项目中需要这个,但我的研究让我相信你不能在 Windows 上做到这一点?我需要一个用于 OSX、Linux 和 Windows 的小脚本。
【问题讨论】:
ipconfig,例如,提取网关信息?
我想你是在问一个XY Problem:你想知道如何通过 python 在 Windows 上找到 DHCP IP 地址?
obtaining DHCP server ip from the command line 在 SuperUser 上有一个解决方案。您可以将ipconfig /all 与subprocess 包装起来,然后解析输出:
import subprocess # Runs a command on the cmd line
res = subprocess.check_output("ipconfig /all")
【讨论】:
好的,我将假设您的默认网关配置为指向您的 DHCP 服务器。我找到了以下包并且能够获得我的默认网关:
#!/usr/bin/env python
import netifaces
gateway_info = netifaces.gateways()
print(gateway_info)
当然,我首先必须通过 pip 安装 netifaces 模块:
$> pip install --user netifaces
代码返回以下内容:
$> ./test3.py
{'default': {2: ('192.168.0.1', 'en0')}, 2: [('192.168.0.1', 'en0', True)]}
我希望这会有所帮助。
最好的问候,
亚伦 C.
【讨论】: