Script to change ip address on windows

Posted on

Question :

Script to change ip address on windows

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.



Asked By: Baz


Answer #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''
subnetmask = u''
gateway = u''

# Set IP address, subnetmask and default gateway
# Note: EnableStatic() and SetGateways() methods require *lists* of values to be passed

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

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.

Answered By: msanders

Answer #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"netsh interface ip set address ....".split())


For those who’s too busy to rtfm,

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

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

Answered By: rocksportrocker

Answer #3:

Actually very simple to do (windows only) (uses preinstalled libraries only):

import os; os.system("ipconfig /renew")
Answered By: mayank1234r56

Answer #4:

You can use vbscript to change IP Address,

Dim strIPAddress, strSubnetMask,strGateway, intGatewayMetric, strDns1, strDns2, objWMIService, colItems, stradaptername, objFSO
Const ForReading = 1 
Const ForAppending = 8
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set OutPutFile = objFSO.CreateTextFile("C:ProgramDatatest.txt" ,2 , True)
Set InterfaceName = objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionStatus >= 0")
If objFSO.FileExists("C:ProgramDatatest.txt") Then
Set OutPutFile = objFSO.CreateTextFile("C:ProgramDatatest1.txt" ,2 , True)
End If
For Each objItem in InterfaceName
If objFSO.FileExists("C:ProgramDatatest.txt") Then
arrInterfaces = objItem.NetConnectionID
'wscript.echo "test: " &arrInterfaces
Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:ProgramDatatest.txt",8,true)
Set objFileToWrite = Nothing
End If
If objFSO.FileExists("C:ProgramDatatest.txt") Then
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:ProgramDatatest.txt",1)
strFileText = objFileToRead.ReadAll()
Set objFileToRead = Nothing
'wscript.echo "obtained" &strFileText
End If
Result = inputbox("Enter the AdapterName: " &vbCrLf &strFileText)
If Result = "" then
'wscript.echo "user selected cancel"
strIPAddress=InputBox("Enter Static IP Adrress: ")
strSubnetMask =InputBox("Enter SubnetMask: " )
strGateway=InputBox("Enter Default Gateway: ")
strDns1=InputBox("Enter Preferred DNS: ")
strDns2=InputBox("Enter Alternate DNS: ")
Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.CurrentDirectory = "C:WindowsSystem32"
objShell.Run "netsh interface ip set address name=""" & Result & """ static " & strIPAddress & " " & strSubnetMask & " " & strGateway & " " & intGatewayMetric, 0, True
objShell.Run "netsh interface ip set dns name=" & Result & " static "& strDns1, 0, True
objShell.Run "netsh interface ip add dns name=" & Result & " addr="& strDns2, 0, True
Set objShell = Nothing: Set obj=Nothing
End If
Answered By: Tester

Leave a Reply

Your email address will not be published. Required fields are marked *