Search code examples
pythonwindowspython-2.7wmiipv4

Python Changing IP Address using WMI


I am attempting to use WMI to change the IP address of my PC. The code below is run from the command line on Windows:

import wmi
c = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=1)
nic = c[1]
ip = "192.168.1.1"
subnetmask = "255.255.255.0"
nic.EnableStatic(IPAddress=[ip], SubnetMask=[subnetmask])

The above code then returns the following result:

(-2147024891,)

I believe this error is related to permissions.

When I run the command line as Administrator, and enter the above code again, I am presented with the value:

(0L,)

This value indicates that the IP change was successful.

Is there a way for me to change the IP address without first running the command line as Administrator?


Solution

  • You can get administrator rights from within the script, as is demonstrated in an other stackoverflow answer