user5275692
user5275692

Reputation: 603

How do I send a DHCP request to find DHCP server IP address? Is this possible?

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.

Upvotes: 3

Views: 2849

Answers (2)

Aaron Couch
Aaron Couch

Reputation: 133

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.

Upvotes: 2

TemporalWolf
TemporalWolf

Reputation: 7952

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")

Upvotes: 0

Related Questions