- ::问题一:提取"默认网关"和DNS1 DNS2 时有的系统版本 DNS2 提取不正常,得到的是dns2=Primary.WINS.Server . . . . . . . : 192.168.74.2 (正常的是:DNS2=192.168.74.2) 请问能否解决???
- ::问题二:本批处理当次可以正常设置,第二次运行批处理后会变为IP和DNS都是自动获取的(好像秕处理不起作用一样),第三次又正常,第四次不正常.....一直循环.....请问是什么原因???如何解决???先谢谢各位大侠。。。。
- @echo off
- ::提取"本地连接"名称
- for /f "tokens=3,4 delims==: " %%i in ('ipconfig ^|findstr /r "本"') do set b=%%i %%j
- echo %b% > bd.txt
- for /f %%a in (bd.txt) do set bd=%%a
- set bdl="%bd%"
- del bd.txt
-
- ::将IP和DNS改为自动获取
- netsh interface ip set address name=%bdl% source=dhcp >nul
- netsh interface ip set dns name=%bdl% source=dhcp >nul
-
- ::提取"默认网关"和DNS1 DNS2
- for /F "tokens=1,2* delims=:. " %%a in ('ipconfig/all') do (
- if defined dns1 set dns2=%%a.%%b.%%c&set d&goto ip
- if /i "%%a %%b"=="DNS Servers" (set "dns1=%%c")else set "dhcp=%%c")
- :ip
-
- ::为使用在同一网段
- echo %dhcp% > dhcp.txt
- for /f "delims=. tokens=1,2,3" %%a in (dhcp.txt) do set dip=%%a.%%b.%%c
- del dhcp.txt
-
- ::防止IP冲突而自动设置IP
- for /l %%i in (2,1,255) do (ping -n 1 %dip%.%%i |find "Reply" >nul 2>nul && echo %dip%.%%i
- 已被使用 || set ip=%dip%.%%i && goto pip )
-
- 设置IP及DNS1 DNS2
- :pip
- set MASK="255.255.255.0"
- set GM=1
- netsh interface ip set address name=%bdl% source=static addr=%IP% mask=%MASK% gateway=%
- dhcp% gwmetric=%GM%
- netsh interface ip set dns name=%bdl% source=static addr=%dns1%
- netsh interface ip set wins name=%bdl% source=static addr=%dns2%
复制代码
|