3

Is it possible to write a small script that will send out a DHCP broadcast request and find the DHCP server address?

I need this for a project, but my research led me to believe that you cannot do this on Windows? I would need a small script for OSX, Linux and Windows.

user5275692
  • 603
  • 1
  • 6
  • 11

2 Answers2

2

Okay, I'm going to make the assumption that your default gateway is configured to point at your DHCP server. I found the following package and was able to get my default gateway:

#!/usr/bin/env python
import netifaces

gateway_info = netifaces.gateways()
print(gateway_info)

I of course first had to install the netifaces module via pip:

$> pip install --user netifaces

Code returns the following:

$> ./test3.py

{'default': {2: ('192.168.0.1', 'en0')}, 2: [('192.168.0.1', 'en0', True)]}

I hope this helps.

Best regards,

Aaron C.

Aaron Couch
  • 113
  • 1
  • 2
  • 7
  • I believe OP wants the IP address of the **DHCP Server** not the client's IP. – sytech Oct 17 '16 at 23:16
  • @Gator_Python I believe you are right. I changed my answer. Assuming his/her default gateway is pointed at the DHCP server then my changes should work. Thanks. – Aaron Couch Oct 17 '16 at 23:36
0

I think you're asking an XY Problem: You want to know how to find the DHCP IP address on windows, via python?

There is a solution on SuperUser for obtaining DHCP server ip from the command line. You can wrap ipconfig /all with subprocess and then parse the output:

import subprocess  # Runs a command on the cmd line

res = subprocess.check_output("ipconfig /all")
Community
  • 1
  • 1
TemporalWolf
  • 7,727
  • 1
  • 30
  • 50