How to modify IP addresses using command lines in Windows (with batch files)

Source: Internet
Author: User

Because I often switch between different networks in my location, such as lan, system Intranet, and Internet (I usually switch between the three ADSL networks only ). I used to set multiple IP addresses of different network segments on the local machine, and then switch the route so that different network segments can go out through different gateways, you can access multiple networks at the same time. However, I often find that some problems may occur, so I decided to use the most primitive method to solve the problem, that is, to use only the IP address of a certain network segment, in this way, you need to constantly change the IP address. Of course, this change in the "Network Connection" attribute of Windows is very troublesome, but fortunately, the "netsh" command of Windows provides the function of changing the IP address under the command line, this makes it much easier to make it a bat batch file.

The following describes the specific methods and commands:

First, enter the command prompt line: "Win + R"> "cmd", and press Enter.
Enter netsh and press Enter. The following message is displayed: netsh>
Enter the interface and press enter to display the netsh interface>
Enter the IP address and press Enter. The netsh interface IP address is displayed.>

Go to the ipsettings. We need to use the following command:
Set address-add an IP address to the specified interface.

Let's take a look at the windows help for 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:

Tag Value
Name-Interface Name.
Source-one of the following values:
DHCP: for a specified interface, configure IP addresses using DHCP.
Address.
Static: Set the IP address using the local static configuration.
Address.

Gateway-one of the following values:
<IP address>: Specifies the default IP address.
Gateway.
None: do not set the default gateway.
Gwmetric-the number of hops of the default gateway. If the gateway is set to 'None ',
This field should not be set.
The following options are set only when 'source' is 'static:

ADDR-IP address of the specified interface.
Mask-specifies the subnet mask of the IP address.

Note: It is used to change the IP Address Configuration Mode from DHCP to static or from static.
Change the mode to DHCP. Use static IP addresses to add IP addresses or
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
----------------------------------------------------------------------------

After understanding the usage, we can set it:
After netsh interface IP>, enter
Set address "Local Connection" static 192.168.4.23 255.255.255.192 192.168.4.1 1
Press enter. "OK" will be displayed later. The IPaddress is set.
Here, "Local Connection" refers to the name of the connection seen in Windows "Network Properties". "static" indicates that the static IP address is to be specified instead of being assigned by DHCP. The next three are IP addresses, subnet Mask, Gateway. The last "1" refers to the number of hops of the default gateway, generally "1 ".

Now, the IP address setting in the command line is complete.
At the same time, you can also change the DNS settings under 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 ):CopyCodeThe Code is as follows: @ echo off
Rem ETH // ETH is the name of the network adapter, which can be queried in the network connection, for example, "Local Connection"
Set Eth = "Local Link"
Rem IP // ip address you want to change
Set IP = 192.168.4.23
Rem GW // GW is the gateway address
Set GW = 192.168.4.1
Rem netmasks // netmasks is the subnet mask
Set netmasks = strongswan 192

ECHO is changing the IP address of the Local Machine: % IP %
REM
If % GW % = none netsh interface IP Set address % ETH % static % IP % netmasks % GW %> NUL
if not % GW % = none netsh interface IP Set address % ETH % static % IP % netmasks % GW % 1> NUL
Echo ....... ..................
ECHO:
ipconfig
echo .........................
echo successfully changed the local IP address to % IP %!
pause
close

Related Article

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.