How to modify an IP address using the command line under Windows (with batch file) _dos/bat

Source: Internet
Author: User
Because I am in a place to often switch between different networks, such as local area network, system intranet and extranet (I will often switch between 3 ADSL networks). I have always used the method is to set up a number of different network segments of the IP, and then switch routes (Route), so that different network segments through different gateways out, you can access multiple networks at the same time to achieve the purpose. However, I found that there are often some problems, so I decided to use the most original way to solve, that is, in order to use a certain segment of the network only use the IP, so you need to constantly replace the IP address. Of course, such changes are cumbersome in Windows ' Network Connections ' properties, but fortunately, Windows ' netsh ' commands the ability to change IP under the command line, which makes it much easier to make a bat batch file.

The following are detailed methods and instructions:

First enter the command prompt line: "win+r" > "cmd", enter
Enter Netsh, carriage return, appear:netsh>
Then enter interface, carriage return, appear netsh interface>
Then enter the IP, carriage return, appear netsh interface ip>

Access to the IP settings, we want to use the following command:
Set address-Adds an IP to the specified interface.

Let's take a look at Windows Help on this order:
--------------------------------------------------------------------------
Usage: Set Address [name=]<string>
[[Source=]dhcp |
[source=] static [ADDR=]IP address [mask=]ip Subnet mask]
[[Gateway=]<ip Address>|none [Gwmetric=]integer]

Parameters:

Tagged value
Name-Interface names.
Source-One of the following values:
DHCP: For specified interfaces, set DHCP to configure IP
Address.
Static: Set IP settings using local static configuration
Address.

Gateway-One of the following values:
<ip address>: Specify default for IP addresses you set
Gateway.
None: Default gateway is not set.
Gwmetric-Metric for the default gateway. If the gateway is set to ' none ', the
This field should not be set.
The following options are set only when ' source ' is ' static ':

Addr-Specifies the IP address of the interface.
Mask-Specifies the subnet mask for the IP address.

Note: Used to change the IP address configuration mode from DHCP mode to static or from static
Change mode to DHCP. Add an IP address to an interface with a static IP address, or add a
The default gateway.
Example:

Set address name= "Local area Connection" Source=dhcp
Set address local static 10.0.0.9 255.0.0.0 10.0.0.1 1
----------------------------------------------------------------------------

Once you know the usage, we can set it up:
Enter after netsh interface ip>
Set Address "Local Area Connection" static 192.168.4.23 255.255.255.192 192.168.4.1 1
Then enter, and then the "OK" is displayed, and the IP settings are complete.
This explains that "local Area Connection" refers to the name of the connection that is seen in Windows Network properties. Static indicates that you want to specify a quiescent IP instead of a DHCP assignment, followed by three IP, subnet mask, Gateway, and finally "1" refers to the default gateway metric, typically "1".

At this point, set IP completion at the command line.
At the same time you can change the DNS settings at the command line, using the set DNS command, you can enter set DNS/? View the Usage method.

Finally, we save these commands as a bat file (setip192168423.bat):
Copy Code code as follows:

@echo off
Rem ETH is the name of the network adapter, which can be queried in a networking connection, such as "Local link"
Set//eth "Local link" eth= R>rem IP//ip for the IP
set ip=192.168.4.23
rem GW//GW you want to change for the gateway address
Set gw=192.168.4.1
rem netmasks//net Masks is the subnet mask
Set netmasks=255.255.255.192

Echo is changing native IP to:%ip%
rem
if%gw%==none netsh inter Face IP set addresses%eth% static%ip%%netmasks%%gw% > nul
if not%gw%==none netsh interface ip set address%eth % static%ip%%netmasks%%gw% 1 > Nul
Echo ...............
Echo checks the current native IP:
ipconfig
echo ... .............
Echo successfully changed the native IP to%ip%!
Pause
Close

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.