标题: [网络连接] 批处理修改网络参数怎样设置DNS和metric? [打印本页]
作者: 7Qp 时间: 2015-6-19 08:55 标题: 批处理修改网络参数怎样设置DNS和metric?
下面这坨是我弄的一个修改网络参数的批,有几个问题
1.修改次要DNS该怎么弄
2.修改DNS的时候总是会提示提供的DNS参数不正确,这是啥原因
3.对metric怎么处理比较合理,现在是跳过这项,直接设成1- @echo off
- :select_int
- cls
- set cnt=0
- echo 请选择网络接口
- echo.
- echo 0.退出
- for /f "tokens=4,5 delims= " %%i in ('netsh int ip sh int') do (
- if "%%i"=="connected" call :int_list %%j
- )
- echo.
- set /p s_int=选择(1-%cnt%,0=退出):
- if "%s_int%"=="" goto select_int
- set tmp=0
- set delm=%tmp%
- :get_list_count
- if not %tmp%==%cnt% (
- call :get_lc
- goto get_list_count
- )
-
- for /f "tokens=1 delims=%delm%" %%a in ('echo %s_int:~0,1%') do (
- if not "%%a"=="" echo 输入有误 && pause>nul && goto select_int)
- if "%s_int:~0,1%"=="0" goto :eof
- set chint=%s_int:~0,1%
- set/a cnt=0
- for /f "tokens=4,5 delims= " %%i in ('netsh int ip sh int') do (
- if "%%i"=="connected" call :int_list2 %%j
- )
-
- set newip=
- set newms=
- set newgw=
- set newdn=
- :show_int
- cls
- echo 网络接口"%c_int%"的参数
- echo.
- echo 0.退出
- for /f "tokens=3 delims=: " %%i in ('netsh int ip sh addr "%c_int%"^|findstr "IP 地址"') do (echo 1.IP 地 址:%%i - %newip% && set ip=%%i)
- for /f "tokens=4 delims=:) " %%j in ('netsh int ip sh addr "%c_int%"^|findstr "子网前缀"') do (echo 2.子网掩码:%%j - %newms% && set ms=%%j)
- for /f "tokens=2 delims=: " %%k in ('netsh int ip sh addr "%c_int%"^|findstr "默认网关"') do (echo 3.网 关:%%k - %newgw% && set gw=%%k)
- for /f "tokens=2 delims=: " %%l in ('netsh int ip sh addr "%c_int%"^|findstr "网关跃点数"') do (echo 4.跃 点 数:%%l && set mr=1)
- for /f "tokens=4 delims=: " %%m in ('netsh int ip sh dns "%c_int%"^|findstr "DNS 服务器"') do (echo 5.主要 DNS:%%m - %newdn% && set dn=%%m)
- echo 6.确认参数并退出
- echo.
- set /p cid=选择对应编号修改参数(1-5,0=不保存退出,6=保存修改并退出):
- if "%cid%"=="" goto show_int
- for /f "tokens=1 delims=0123456" %%a in ('echo %cid:~0,1%') do (
- if not "%%a"=="" echo 输入有误 && pause>nul && goto show_int)
- if "%cid:~0,1%"=="0" goto :eof
- if "%cid:~0,1%"=="6" goto confimpara
- if "%cid:~0,1%"=="4" goto show_int
- call :newpara %cid:~0,1%
- goto show_int
-
- :confimpara
- echo.
- echo.
- echo.
- if not "%newip%"=="" set ip=%newip%
- if not "%newms%"=="" set ms=%newms%
- if not "%newgw%"=="" set gw=%newgw%
- if not "%newdn%"=="" set dn=%newdn%
- echo 错误信息:(如无提示即正确)
- netsh int ip set addr %c_int% static %ip% %ms% %gw% %mr%
- netsh int ip set dns %c_int% static %dn% primary
- echo.
- echo.
- echo.
- pause
- goto :eof
-
- :newpara
- if "%1"=="1" set str=IP地址
- if "%1"=="2" set str=子网掩码
- if "%1"=="3" set str=网关
- if "%1"=="5" set str=主DNS服务器地址
- set /p newp=请输入新%str%:
- if "%newp%"=="" goto :eof
- for /f "tokens=1 delims=0123456789." %%a in ('echo %newp%') do (
- if not "%%a"=="" echo 输入有误
- pause>nul
- set/a newp=
- goto :eof
- )
- for /f "tokens=1,2,3,4,5 delims=." %%b in ('echo %newp%') do (
- if %%b lss 0 (echo 第一位小于0&&pause>nul&&set/a newp=)
- if %%b gtr 255 (echo 第一位大于255&&pause>nul&&set/a newp=)
- if %%c lss 0 (echo 第二位小于0&&pause>nul&&set/a newp=)
- if %%c gtr 255 (echo 第二位大于255&&pause>nul&&set/a newp=)
- if %%d lss 0 (echo 第三位小于0&&pause>nul&&set/a newp=)
- if %%d gtr 255 (echo 第三位大于255&&pause>nul&&set/a newp=)
- if %%e lss 0 (echo 第四位小于0&&pause>nul&&set/a newp=)
- if %%e gtr 255 (echo 第四位大于255&&pause>nul&&set/a newp=)
- if not "%%f"=="" (echo 输入有误&&pause>nul&&set/a newp=)
- )
- if "%1"=="1" set newip=%newp%
- if "%1"=="2" set newms=%newp%
- if "%1"=="3" set newgw=%newp%
- if "%1"=="5" set newdn=%newp%
- goto :eof
-
- :int_list
- set /a cnt+=1
- echo %cnt%.%1
- goto :eof
-
- :int_list2
- set /a cnt+=1
- if "%cnt%"=="%chint%" set c_int=%1
- goto :eof
-
- :get_lc
- set /a tmp+=1
- set delm=%delm%%tmp%
- goto :eof
-
- :eof
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |