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

K

kode4u

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?
 
D

Duncan Booth

kode4u said:
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
(False, '192.168.126.1', '255.255.255.0')
 
K

kode4u

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 said:
kode4u said:
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
(False, '192.168.126.1', '255.255.255.0')
 
R

Roger Upole

kode4u said:
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?

You can use WMI to list properties of your network adapter(s):
import win32com.client
wmi=win32com.client.GetObject('winmgmts:')
adapters=wmi.InstancesOf('Win32_NetworkAdapterConfiguration')
for adapter in adapters:
print adapter.Properties_['Caption'], adapter.Properties_['DhcpEnabled'], adapter.Properties_['IpAddress']

Roger
 
K

kode4u

There are some errors in result:.... print adapter.Properties_['Caption'],
adapter.Properties_['DhcpEnabled'], adapter.Properties_['IpAddress']
....
[00000001] Realtek RTL8139 Family PCI Fast Ethernet NIC True
(u'169.254.52.213',)
Traceback (most recent call last):
File "<input>", line 2, in <module>
File "C:\Python25\Lib\site-packages\win32com\client\dynamic.py", line
187, in __str__
return str(self.__call__())
UnicodeEncodeError: 'ascii' codec can't encode characters in position
15-19: ordinal not in range(128)

How to deal with it?



Roger said:
kode4u said:
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?

You can use WMI to list properties of your network adapter(s):
import win32com.client
wmi=win32com.client.GetObject('winmgmts:')
adapters=wmi.InstancesOf('Win32_NetworkAdapterConfiguration')
for adapter in adapters:
print adapter.Properties_['Caption'], adapter.Properties_['DhcpEnabled'], adapter.Properties_['IpAddress']

Roger
 
R

Roger Upole

This means the unicode object contains characters that
can't be represented in the default encoding. Try encoding it
in another character set, something like

adapter.Properties_['Caption'].Value.encode('mbcs')
(you may need to use a different codec)

Roger


kode4u said:
There are some errors in result:... print adapter.Properties_['Caption'],
adapter.Properties_['DhcpEnabled'], adapter.Properties_['IpAddress']
...
[00000001] Realtek RTL8139 Family PCI Fast Ethernet NIC True
(u'169.254.52.213',)
Traceback (most recent call last):
File "<input>", line 2, in <module>
File "C:\Python25\Lib\site-packages\win32com\client\dynamic.py", line
187, in __str__
return str(self.__call__())
UnicodeEncodeError: 'ascii' codec can't encode characters in position
15-19: ordinal not in range(128)

How to deal with it?



Roger said:
kode4u said:
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?

You can use WMI to list properties of your network adapter(s):
import win32com.client
wmi=win32com.client.GetObject('winmgmts:')
adapters=wmi.InstancesOf('Win32_NetworkAdapterConfiguration')
for adapter in adapters:
print adapter.Properties_['Caption'], adapter.Properties_['DhcpEnabled'], adapter.Properties_['IpAddress']

Roger
 
K

kode4u

Thank you very much.

Does the wmi object can modify the ip address?

Roger said:
This means the unicode object contains characters that
can't be represented in the default encoding. Try encoding it
in another character set, something like

adapter.Properties_['Caption'].Value.encode('mbcs')
(you may need to use a different codec)

Roger


kode4u said:
There are some errors in result:
import win32com.client
wmi=win32com.client.GetObject('winmgmts:')
adapters=wmi.InstancesOf('Win32_NetworkAdapterConfiguration')
for adapter in adapters:
... print adapter.Properties_['Caption'],
adapter.Properties_['DhcpEnabled'], adapter.Properties_['IpAddress']
...
[00000001] Realtek RTL8139 Family PCI Fast Ethernet NIC True
(u'169.254.52.213',)
Traceback (most recent call last):
File "<input>", line 2, in <module>
File "C:\Python25\Lib\site-packages\win32com\client\dynamic.py", line
187, in __str__
return str(self.__call__())
UnicodeEncodeError: 'ascii' codec can't encode characters in position
15-19: ordinal not in range(128)

How to deal with it?



Roger said:
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?


You can use WMI to list properties of your network adapter(s):
import win32com.client
wmi=win32com.client.GetObject('winmgmts:')
adapters=wmi.InstancesOf('Win32_NetworkAdapterConfiguration')
for adapter in adapters:
print adapter.Properties_['Caption'], adapter.Properties_['DhcpEnabled'], adapter.Properties_['IpAddress']

Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top