How to get ip setting, dynamic ip or static ip?
kode4u
kode4u at gmail.com
Thu Sep 21 22:03:51 EDT 2006
The way has one shortcoming. That's it depend on the proper language
version of Windows. If I'm using Simplified Chinese Windows, I must
modify some strings.
:(
Duncan Booth wrote:
> "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