Automatically obtain local connection and modify network address

bat file processing

bat file processing instance
automatically obtains local connection and network address modification


foreword

The product development of the company's LAN or fixed network address often encounters the problem of modifying the address. It is very troublesome to use it manually every time, and it is necessary to remember the fixed IPv4 address. It is a common thing for developers to modify it, but once the product is mass-produced and handed over to other departments for use and testing, there will be problems with network addresses that do not correspond. Often, the problems reported by other departments are always problems with the upper computer or the lower computer. , making people bald.
This article can save the I local network P address in the bat file under windows to the current bat file and set the I local network P address to automatically obtain the I local network P address and other functions.


1. Basic grammar of Windows command line CMD

grammar help

All CMD commands can be used at the same time 命令 /?to get help. It can also be used directly help 命令.
First, enter the Windows command line and perform the following operations:
hold down the win + r key to enter cmd and press Enter
[image upload failed...(image-e99b16-1655605490569)]
and then enter ping /? to query the ping command use help.
[Image upload failed...(image-a5e7de-1655605490569)]

variable assignment

Direct use of set operations without defining variable types

set j=0

Title loop, conditional judgment

The statement syntax for conditional judgment is as follows:

IF  [判断条件]  (
       [命令1]
    ) ELSE (
       [命令2]
    )

The judgment condition can be a normal variable k==1or the like, or a character string string1==string2.
Comparison conditions can also be expressed with the following syntax

EQU - 等于
NEQ - 不等于
LSS - 小于
LEQ - 小于或等于
GTR - 大于
GEQ - 大于或等于

It is more commonly used to judge whether the file exists, as follows, if the file 1.txt exists, delete it

IF EXIST 1.txt del 1.txt 

The FOR loop statement is often used to train each line in a file in rotation, or to divide each line of strings with certain symbols and then perform a round-robin search.
The basic syntax is as follows
Rotation training outputs each line of strings in 1.txt, and tokens represent the value matched by the wildcard * when the %%t variable is assigned.

for /f "tokens=*" %%t in (1.txt) do (
echo %%t

variable call

When assigning a value, you can directly assign the variable value j to the variable b, and you need to add %j% when calling.

set j=0
set b=j
set con%j%=1

Required when adding variable delay operations! j! make a variable call

setlocal enabledelayedexpansion
set j=0
set b=j
set con!j!=1

For other usage syntax, please refer to other people's blog CMD basic syntax

Two, bat file processing code interpretation

1. Initial operation

code show as below:

@set "interipp=192.168.31.142"
@set "intermaskk=255.255.255.0"
@set "intergatee=192.168.31.1"
@set "interdns11=192.168.31.1 "
@set "interdns22=172.16.3.200"
@echo off
@cls

At the beginning, what was saved here was the address we used to record the local network P of the local machine I, and it was put here for non-use! And the escape character ^, because this character will be escaped into a variable value by default by the system when generating the bat file, so the regenerated bat file cannot be used. (This kind of debugging has been done for a long time, and there is no good way to deal with it on the Internet.)

2. Get the local connection name

::------------------------------------先跳转到这里---------------------------------------------------------------
::获取网络连接名称
:GETNAME
@if exist 10.bat del 10.bat
@cls
@(for /f "tokens=3*" %%a in ('netsh interface show interface^|more +2') do @echo,%%b)>".\network.txt"
@set j=0
@echo 获取到以下本地连接:
@for /f "tokens=*" %%i in (.\network.txt) do (
    set /a j+=1
    call set 网络连接%%j%%=%%i
    call set ethernetAdapter=%%i
    call echo %%ethernetAdapter%%
)
@del /s /q /f .\network.txt

The above code implements the query of the local network connection, and saves it in a network.txt for output and printing. After the output is printed, delete the file (the last line).

3. Print local network connection

::选择需要设置的本地连接
@cls
@echo ----------------------------------------------------------------
@echo ----------------------------------------------------------------
@echo 请选择需要设置的网络连接:
@for /l  %%i in (1,1,!j!) do (
echo 请按 %%i设置   "!网络连接%%i!"
)
echo.

3. Select the local connection to be operated

::根据选择赋值
set /p KEYSET= 已选择:
@cls
@for /l  %%i in (1,1,!j!) do (
if %KEYSET% == %%i  set name=!网络连接%%i!
if %KEYSET% == %%i  echo 已选择  "!网络连接%%i!"
)
goto SET

This part is added because there will be multiple local connections when a computer is installed with a virtual machine or a wireless connection. Only if you choose the correct local connection can you set the correct I local network P connection network.

4. Set the LAN fixed network address

:SET
@echo ----------------------------------------------------------------
@echo ----------------------------------------------------------------
echo 设置[局域网I P]请按 1
echo 设置[自动I.P]请按 2
echo 设置[固定]请按 3
echo 设置双I.P 本机+[固定]请按 4
echo 记录当前本机I P为[局域网I.P]请按 d
echo.
@set /p KEY= 已选择:
if %KEY% == 1 goto ONE
if %KEY% == 2 goto TWO
if %KEY% == 3 goto THREE
if %KEY% == 4 goto FOUR
if %KEY% == 5 goto FIVE
if %KEY% == d goto D

The above is used to select the jump, and if you select d, you will jump to the option to save the current IP.

5. Dynamically assign network addresses

The code is as follows (example):

:TWO
echo 正在进行动态IP设置,请稍等...   
echo. IP 地址正在从DHCP自动获取...   
netsh interface ip set address "%name%" dhcp   
echo. DNS地址正在从DHCP自动获取...   
netsh interface ip set dns "%name%" dhcp   
echo ----   
echo 全部设置完成!   
pause  
goto end

This part is very common and there are many on the Internet, so I won’t introduce it here.

6. Add double local network address

The code is as follows (example):

:THREE
echo 设置7 正在自动添加 I.P ......
netsh interface ip add address name="%name%"   addr=172.29.4.100 mask=255.255.0.0   
echo I.P地址 /子网掩码 /网关设置完成
pause
goto end

7. Save the local IPv4 address in the current bat file

This part will first obtain the local connection IP address, and then jump to

for /f "tokens=3* delims=: " %%a in ('netsh interface ip show config "%adapter%"^|find /i "IP 地址"') do (
set defaultip=%%a
goto NEXT1
)
:NEXT1
@for /f "tokens=4* delims=) " %%a in ('netsh interface ip show config "%adapter%"^|find /i "掩码"') do (
set defaultmask=%%a
goto NEXT2
)

The part below will read and output the current bat file, and the output will match the string of the line where DNS2 is located. If it exists, all output will be cleared, and the IP field will be output separately (in order to avoid exclamation marks and other escape characters when generating the bat file This is why the local IP address is placed in the first few lines of the bat file), and then the subsequent output is consistent with the content of the current bat file.

@(for /f "tokens=* delims=" %%t in ('findstr  .* %infile%') do (
@for /f "tokens=1,2* delims= " %%i in ("%%t") do (
@rem 匹配dns1
@if "%%~j"=="interdns22=%interdns22%" (
@if exist %outfile% del %outfile%
@echo @set "interipp=%defaultip%">>%outfile%
@echo @set "intermaskk=%defaultmask%">>%outfile%
@echo @set "intergatee=%defaultgate%">>%outfile%
@echo @set "interdns11=%defaultdns1%">>%outfile%
@set /a cnt=1
)
)

Finally, there is a jump to generate a new bat file to delete the current bat file and rename the newly generated bat file to the original bat file name.

echo 保存完成
goto JUMP
::生成一个新的bat文件进行旧文件的删除以及新文件的命名
:JUMP
@echo. >10.bat
@echo if exist %infile% del %infile%>>10.bat
@echo ren 3.bat %infile%>>10.bat
@echo %infile%>>10.bat
@echo if exist 10.bat del 10.bat>>10.bat
@echo exit>>10.bat
10.bat
exit

source file

The source code file has been uploaded, and the settings can be downloaded.
Resource file: local connection address is automatically modified and saved


Summarize

bat file processing is very convenient, some basic operations of adding and deleting files and computer settings can be set through this, such as automatic shutdown, adding scheduled tasks, etc.

Guess you like

Origin blog.csdn.net/weixin_43058521/article/details/125355499