How to get ip setting, dynamic ip or static ip?

Duncan Booth duncan.booth at invalid.invalid
Thu Sep 21 10:04:25 EDT 2006


"kode4u" <kode4u at gmail.com> wrote:

> How to use python get my windows box's ip setting type? Dynamic ip, or
> static ip?
> 
> If it's static ip, what's the exact value?
> 
Here's one way:

def ipinfo(interface="Local Area Connection"):
	dhcpenabled = False
	staticip = None
	subnetmask = None
	for l in os.popen('netsh interface ip show address "%s"' % interface):
		l = l.strip()
		if l.startswith('DHCP enabled'):
			dhcpenabled = l.endswith('Yes')
		if l.startswith("IP Address"):
			staticip = l.rsplit(None,1)[-1]
		if l.startswith("SubnetMask"):
			subnetmask = l.rsplit(None,1)[-1]
	return dhcpenabled, staticip, subnetmask

>>> ipinfo()
(True, None, None)
>>> ipinfo("VMware Network Adapter VMnet1")
(False, '192.168.126.1', '255.255.255.0')



More information about the Python-list mailing list