Script to change ip address on windows

44,766

Solution 1

You can use the Python WMI module to do this (install the PyWin32 extensions and the WMI module before running these scripts). Here is how to configure things to talk to the hardware device:

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])

Here is how to revert to obtaining an IP address automatically (via DHCP):

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()

Note: in a production script you should check the values returned by EnableStatic(), SetGateways() and EnableDHCP(). ('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).

Full information on all the functionality of the Win32NetworkAdapterConfiguration class can also be found on MSDN.

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.

Solution 2

You can use the subprocess module to start

netsh interface ip set address [params]

Start this from the commandline (without[params]) to get some help how to use it. Then you can do

import subprocess
subprocess.call("netsh interface ip set address ....".split())

Update:

For those who's too busy to rtfm,

netsh interface ip set address lan static 192.168.0.100 255.255.255.0
netsh interface ip set address lan dhcp

here lan is the name of the network interface to configure, 192.168.0.100 is ip address, 255.255.255.0 is network mask. The first command sets static address, the second reverts to dhcp.

Share:
44,766
Baz
Author by

Baz

Updated on May 27, 2021

Comments

  • Baz
    Baz almost 3 years

    I use my computer to communicate with a piece of hardware via ethernet. To communicate with this device I set my ip to 192 168 0 11, subnet mask to 255 255 255 0, and default gateway to 192 168 0 1 for IPv4. To use the internet, I choose "Obtain an IP address automatically" via control panel.

    I'd like to have a script that allows my to quickly choose one or the other ethernet setting - hardware or internet.

    I program mostly in python but maybe there is a batch file solution.

    Thanks,

    Barry.

  • Antony Hatchkins
    Antony Hatchkins about 5 years
    I've updated the answer with some details, hope you don't mind ;)
  • Antony Hatchkins
    Antony Hatchkins about 5 years
    Nice answer, except that it's not clear how to tell between several network interfaces if there're more than one (nic.Description for that) and that the script must be called with Administrator rights (see stackoverflow.com/questions/6811372/…)