Code for obtaining the NIC name through batch processing (with a script for modifying the IP address, gateway, and DNS)

Source: Internet
Author: User

Copy codeThe Code is as follows: Use the ipconfig/all command to obtain the NIC name.
FOR/F "tokens = 2 *" % I IN ('ipconfig/all ^ | find/I "Ethernet adapter" ') DO set name = % j
: Use the for command to delete the colon following the NIC name.
FOR/F "tokens = 1 * delims =:" % I in ("% name %") do set Nic name = % I

Apply the above script to modify the DNS. For example, if the DNS server in China is paralyzed yesterday, change DNS to 202.96.199.132 to restore normal internet access.

Modify DNS FragmentCopy codeThe Code is as follows: FOR/F "tokens = 2 *" % I IN ('ipconfig/all ^ | find/I "Ethernet adapter" ') DO set name = % j
FOR/F "tokens = 1 * delims =:" % I in ("% name %") do set Nic name = % I
Netsh-c interface ip add dns name = "% Nic name %" addr = 202.96.199.132 index = 1
Netsh-c interface ip add dns name = "% Nic name %" addr = 202.96.64.38 index = 2

Modify IP address, subnet mask, gateway, and DNS codeCopy codeThe Code is as follows: FOR/F "tokens = 2 *" % I IN ('ipconfig/all ^ | find/I "Ethernet adapter" ') DO set name = % j
FOR/F "tokens = 1 * delims =:" % I in ("% name %") do set Nic name = % I
Netsh-c interface ip set address name = "% Nic name %" source = static addr = 192.168.1.215 mask = 255.255.255.0
Netsh-c interface ip set address name = "% Nic name %" gateway = 192.168.1.1 gwmetric = 0
Netsh-c interface ip add dns name = "% Nic name %" addr = 202.96.209.20.index = 1
Netsh-c interface ip add dns name = "% Nic name %" addr = 202.96.209.5 index = 2

The test just now showed that if there are two NICs, the smaller number next to the local connection will be changed. For example, there are two local connections named "local connection 2" and "local connection 3", respectively, the script will modify the local connection 2 and cannot correctly identify the active network card, so it is not perfect. I can only wait for LZ to help improve it.

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.