如何发送DHCP请求以查找DHCP服务器IP地址?这可能吗?

时间:2016-10-17 22:23:09

标签: python dhcp

是否可以编写一个发送DHCP广播请求并找到DHCP服务器地址的小脚本?

我需要这个项目,但是我的研究让我相信你不能在Windows上做到这一点?我需要一个适用于OSX,Linux和Windows的小脚本。

2 个答案:

答案 0 :(得分:0)

我认为你问XY Problem:你想知道如何通过python在Windows上找到DHCP IP地址?

SuperUser上有obtaining DHCP server ip from the command line的解决方案。您可以使用ipconfig /all包装subprocess,然后解析输出:

import subprocess  # Runs a command on the cmd line

res = subprocess.check_output("ipconfig /all")

答案 1 :(得分:0)

好的,我将假设你的默认网关配置为指向你的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)]}

我希望这会有所帮助。

致以最诚挚的问候,

Aaron C。

相关问题