473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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?

Sep 21 '06 #1
6 6517
"kode4u" <ko****@gmail.comwrote:
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')
Sep 21 '06 #2
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" <ko****@gmail.comwrote:
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')
Sep 22 '06 #3
"kode4u" <ko****@gmail.comwrote in message news:11*********************@b28g2000cwb.googlegro ups.com...
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_NetworkAdapterConf iguration')
for adapter in adapters:
print adapter.Properties_['Caption'], adapter.Properties_['DhcpEnabled'], adapter.Properties_['IpAddress']

Roger


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Sep 22 '06 #4
There are some errors in result:
>>import win32com.client
wmi=win32com.client.GetObject('winmgmts:')
adapters=wmi.InstancesOf('Win32_NetworkAdapterCo nfiguration')
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 Upole wrote:
"kode4u" <ko****@gmail.comwrote in message news:11*********************@b28g2000cwb.googlegro ups.com...
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_NetworkAdapterConf iguration')
for adapter in adapters:
print adapter.Properties_['Caption'], adapter.Properties_['DhcpEnabled'], adapter.Properties_['IpAddress']

Roger


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Sep 27 '06 #5
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" <ko****@gmail.comwrote in message news:11**********************@m7g2000cwm.googlegro ups.com...
There are some errors in result:
>>>import win32com.client
wmi=win32com.client.GetObject('winmgmts:')
adapters=wmi.InstancesOf('Win32_NetworkAdapterC onfiguration')
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 Upole wrote:
>"kode4u" <ko****@gmail.comwrote in message news:11*********************@b28g2000cwb.googlegro ups.com...
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_NetworkAdapterCon figuration')
for adapter in adapters:
print adapter.Properties_['Caption'], adapter.Properties_['DhcpEnabled'], adapter.Properties_['IpAddress']

Roger


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---


Sep 27 '06 #6
Thank you very much.

Does the wmi object can modify the ip address?

Roger Upole wrote:
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" <ko****@gmail.comwrote in message news:11**********************@m7g2000cwm.googlegro ups.com...
There are some errors in result:
>>import win32com.client
wmi=win32com.client.GetObject('winmgmts:')
adapters=wmi.InstancesOf('Win32_NetworkAdapterCo nfiguration')
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 Upole wrote:
"kode4u" <ko****@gmail.comwrote in message news:11*********************@b28g2000cwb.googlegro ups.com...
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_NetworkAdapterConf iguration')
for adapter in adapters:
print adapter.Properties_['Caption'], adapter.Properties_['DhcpEnabled'], adapter.Properties_['IpAddress']

Roger


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Sep 29 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
2
by: James | last post by:
Hello, I have an existing MFC C++ project which I need to add the ability of sending WSDL calls out to a web service. When this project is deployed the web service URL will always change...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
2
by: SE.Computerguy | last post by:
I have a webcontrols menu on my webform (masterpage) that does not display the font color properties I set. In the IDE I set the properties and can see them in the html code but when I run the...
6
by: =?ISO-8859-1?Q?Tim_B=FCthe?= | last post by:
Hi, we are building a Java webapplication using JSF, running on websphere, querying a DB2 9 on Suse Enterprise 10. The app uses JDBC and PreparedStatements only (aka dynamic SQL). Every night,...
5
by: bearophileHUGS | last post by:
I often use Python to write small programs, in the range of 50-500 lines of code. For example to process some bioinformatics data, perform some data munging, to apply a randomized optimization...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.