Friday, May 16, 2008

Windows ip config with batch scripts

It is possible to configure TCP/IP with simple batch scripts in Windows XP, even though the information is well hidden. There are a few typical Windows annoyances attached, but for a notebook user who needs to change settings often, double-clicking a batch file is definitely much better than the network configuration GUI.

The almost secret - even though it is included in the default XP install - tool is called netsh.

Below are a few examples. The main idiosyncrasy is that you need the name of the interface to configure, and that name is somewhat unpredictable. In particular, it is localized. For the normal Ethernet interface, in an English system, it starts with "Local Area Connection". But it is different in other language versions of Windows. There is no simple standard name like "eth0" or "en0".

Look up your interface names in "Network Connections", or by typing ipconfig or
netsh interface show interface
at the command prompt.

A simple batch file which will set your LAN interface to DHCP:
SET LAN=Local Area Connection

netsh interface ip set address name="%LAN%" source=dhcp
netsh interface ip set dns name="%LAN%" source=dhcp register=NONE
netsh interface ip set wins name="%LAN%" source=dhcp

This one will set it to a fixed IP address:
SET LAN=Local Area Connection

SET IP=192.168.1.56
SET MASK=255.255.255.0

SET GW=192.168.1.1
SET DNS=%GW%

netsh interface ip set address name="%LAN%" source=static addr=%IP% mask=%MASK%
netsh interface ip set address name="%LAN%" gateway=%GW% gwmetric=0
netsh interface ip set dns name="%LAN%" source=static addr=%DNS% register=NONE

Finally, a more complete example, which sets both the LAN and WIFI interfaces can take an optional second DNS server, and set the WINS server:
SET LAN=Local Area Connection 5
SET WIFI=Wireless

SET IPLAN=192.168.1.56
SET MASKLAN=255.255.255.0

SET IPWIFI=192.168.1.57
SET MASKWIFI=255.255.255.0

SET GW=192.168.1.1
SET DNS=192.168.1.1
SET DNS2=
SET WINS=none

netsh interface ip set address name="%LAN%" source=static addr=%IPLAN% mask=%MASKLAN%
netsh interface ip set address name="%LAN%" gateway=%GW% gwmetric=0
netsh interface ip set dns name="%LAN%" source=static addr=%DNS% register=NONE
IF NOT "%DNS2%" == "" netsh interface ip add dns name="%LAN%" addr=%DNS2% index=2
netsh interface ip set wins name="%LAN%" source=static addr=%WINS%

netsh interface ip set address name="%WIFI%" source=static addr=%IPWIFI% mask=%MASKWIFI%
netsh interface ip set address name="%WIFI%" gateway=%GW% gwmetric=0
netsh interface ip set dns name="%WIFI%" source=static addr=%DNS% register=NONE
IF NOT "%DNS2%" == "" netsh interface ip add dns name="%WIFI%" addr=%DNS2% index=2
netsh interface ip set wins name="%WIFI%" source=static addr=none
netsh interface ip set wins name="%WIFI%" source=static addr=%WINS%


You can also use netsh to configure the Windows firewall, check network connectivity, etc.

As a ping replacement, try this:
netsh diag ping adapter 1
(replace "1" with your network adapter's index number, which you can find with netsh diag show adapter). It will automatically ping your gateway, your DNS server(s) and your own IP address.

For more information, try these links:
How can I configure TCP/IP settings from the Command Prompt?
or
10 things you should know about the NETSH tool
or just do a web search on "netsh".

Labels: , , , , ,

2 Comments:

Blogger nazar said...

Thanks For Your information about the ip config.I find the internet ip address from the site www.ip-details.com.From your article I know about the ip config is find Local server ip address.Thank U.

29 July, 2009 06:10  
Anonymous Wes said...

Thanks for the batch scripts, makes life much easier.

02 December, 2019 12:13  

Post a Comment

<< Home