Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use the Python <a href="http://timgolden.me.uk/python/wmi/index.html">WMI module</a> to do this (install the <a href="http://starship.python.net/crew/mhammond/win32/Downloads.html">PyWin32 extensions</a> and the WMI module before running these scripts). Here is how to configure things to talk to the hardware device:</p> <pre><code>import wmi # Obtain network adaptors configurations nic_configs = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True) # First network adaptor nic = nic_configs[0] # IP address, subnetmask and gateway values should be unicode objects ip = u'192.168.0.11' subnetmask = u'255.255.255.0' gateway = u'192.168.0.1' # Set IP address, subnetmask and default gateway # Note: EnableStatic() and SetGateways() methods require *lists* of values to be passed nic.EnableStatic(IPAddress=[ip],SubnetMask=[subnetmask]) nic.SetGateways(DefaultIPGateway=[gateway]) </code></pre> <p>Here is how to revert to obtaining an IP address automatically (via DHCP):</p> <pre><code>import wmi # Obtain network adaptors configurations nic_configs = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True) # First network adaptor nic = nic_configs[0] # Enable DHCP nic.EnableDHCP() </code></pre> <p>Note: in a production script you should check the values returned by <a href="http://msdn.microsoft.com/en-us/library/aa390383%28v=VS.85%29.aspx">EnableStatic()</a>, <a href="http://msdn.microsoft.com/en-us/library/aa393301%28v=VS.85%29.aspx">SetGateways()</a> and <a href="http://msdn.microsoft.com/en-us/library/aa390378%28v=VS.85%29.aspx">EnableDHCP()</a>. ('0' means success, '1' means reboot required and other values are described on the MSDN pages linked to by the method names. Note: for EnableStatic() and SetGateways(), the error codes are returned as lists).</p> <p>Full information on all the functionality of the Win32NetworkAdapterConfiguration class can also be <a href="http://msdn.microsoft.com/en-us/library/aa394217%28VS.85%29.aspx">found on MSDN</a>.</p> <p>Note: I tested this with Python 2.7, but as PyWIn32 and WMI modules are available for Python 3, I believe you should be able to get this working for Python 3 by removing the "u" from before the string literals.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload