Batch modify IP address bat file processing code in windows

Source: Internet
Author: User

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.

 

@ 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 local ip address to: % 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 checks the current local ip Address:
Ipconfig
Echo .........................
Echo successfully changed the local ip address to % ip %!
Pause
Close


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

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.