Windows through BAT to get the actual name of the network connection, strengthen the IP class settings script compatibility _dos/bat

Source: Internet
Author: User

One of the more difficult problems to solve is to get the actual name of the network connection, most people use the IP settings script to write "local connection" in the code, but in fact not all computers are called local connection, for example, some user computers may upgrade the network card driver, or replace the network card, resulting in the Internet connection name becomes " Local Connection 2 ", such as a boring good person (a production line of the operator computer), will be the name of the network of various DIY, I have seen a modified into a" fucking youth ", an instant without words!

All right, no more nonsense, just serve the dishes!


For the actual name of the network connection, what may occur is:
1. Network card Replacement/driver upgrade results in name changes, such as local connection 2
2. The system language different causes the network line name also to be different, for example simplified is called "The local Connection", but the traditional is called "the area Link"
3. The system version is different, resulting in different command line display, such as Ipconfig/all, simplified XP display as "Ethernet adapter local Connection:" And the simplified WIN7 display is "Ethernet Adapter local Connection:"
4. Other unforeseen issues

If you want to write a generic IP modification script, you must take into account the above 1-3 issues, to improve compatibility, close to version/Jianfan language! (English version not considered)
(PS: The following code gets the name only for the description, other basics show):

First, give an XP under the simple traditional common to get the line name code

@echo off  
for/f "tokens=1,2,3 delims=:"%%i in (' Ipconfig/all ^|find/i "Ethernet adapter") do set Netname=%%k 
netsh interface ip set address name= "%netname%" source=static addr=192.168.1.105 mask=255.255.254.0 gateway=192.168.1.1  
Pause

Simple explanation: This code obtains the connection information through the Ipconfig/all, then crawls Ethernet adapter after the name, namely%%k for the actual line name, theoretically simple traditional general.

Code disadvantage:
1. Incompatible win7,win7 using Ipconfig/all, get the "Ethernet Card Area Link:"
2. There should be an error when more than one network card is local (no actual measurement)
Applicable occasions: In the guarantee of single card and XP environment can be used without distinction between simple and traditional use.

Second, to a xp/win7/simple traditional take-all line name to get code (recently only to help maintenance personnel write, warm!) ):

REM content slightly  
for/f tokens^=1^,2^ delims^=^ "%%j in (' netsh interface ip show config ') do (    
    if"%%j "=" Interface "( 
    net SH interface ip set address name= "%%k" source=static addr=192.168.1.105 mask=255.255.254.0   
  ) 
    if "%%j" = = "Interface" ( 
    netsh interface ip set address name= "%%k" Source=static addr=192.168.1.105 mask=255.255.254.0 G ateway=192.168.1.1  
   )  
)

Simple description: Through Netsh to obtain information, can be compatible with XP and WIN7, and then through the "interface" or "interfaces" of the distinction, but also compatible with simple traditional, so it is all-in-one code!

Code drawback: No more than the implementation of multiple network card differentiation function, so dual/multiple network card computer to execute this script, all network cards are set to the same information, this very egg pain!
Applicable scope: As long as there is no multiple network card, can be used! Multi-nic careful use Oh ~!

Third, finally, to an XP environment to add a multiple network card to determine the name of the script bar!
Ps: Maybe a package can be referenced to make a fully compatible script! (I'm too tight, I don't have to toss.)

REM Call this module using CALL:GETNETNME and get the line name from the temporary file "%~dp0dns.tmp"!  
: Getnetname  
setlocal enabledelayedexpansion  
set "Nic_flag=ethernet"  
set nic_num=0  
for/f "tokens=1,2,3,* delims=:"%%i in (' Ipconfig/all ') do (  
  set /a num+=1  
  if "%%i" = "%nic_flag%" (  
    set/a num_=!num!+3,nic_num+=1  
    set nic_name! NIC_NUM!=%%K)  
  If!num! EQU!num_! Set var=%%l 
  set var=!var:*:=!  
  Set desp! nic_num!=!var!  
)  
If%nic_num% GTR 1 (Echo found multiple network adapters, please manually set ...  
     ) Pause>nul  
     goto tcpset  
     )  
for/l%%i in (1 1%nic_num%) do echo! Nic_name%%i!> "%~dp0dns.tmp" 

Simple description: In the code one based on the number of cards added to the judgment, more than a hint of manual settings, to avoid the problems caused by multiple network card settings IP.

Code disadvantage:

1. Or failed to resolve multiple network card automatically set IP situation, please be interested in the package reference improvement Oh!
2. Incompatible WIN7

OK, the above is to get the actual network line name of all the code, I hope to help a friend in need!

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.