标题: [网络连接] 也发个网络设置相关批处理代码 [打印本页]
作者: terse 时间: 2014-10-12 00:53 标题: 也发个网络设置相关批处理代码
代码不是考虑很全面,权当参考,
由于测试环境不全;代码不免存在问题;望指正。- @echo off
- set "str=DefaultIPGateway^,IPAddress^,DNSServerSearchOrder^,IPSubnet^,DHCPEnabled"
-
- for /f "skip=1tokens=1*" %%i in ('wmic Nic where "Manufacturer<>'Microsoft' and NetConnectionStatus='2'" get Index^,NetConnectionID^') do (
- setlocal enabledelayedexpansion
- for /f %%j in ("%%j") do (
- set $%%i=%%j&echo 适配器 Index:[%%i] : 适配器名:"%%j"
- for /f "tokens=1-3delims={}," %%a in ('wmic Nicconfig where "Index='%%i'" get %str% /value') do (
- for /f %%c in (%%c) do if /i "%%a" == "DNSServerSearchOrder=" set "DNS2=%%c"
- for /f %%c in ("%%a%%~b") do set "%%c"
- )
- echo;
- if /i "!DHCPEnabled!" == "TRUE" (
- echo 获取IP地址模式:自动
- ) else echo 获取IP地址模式:手动
- echo ip地址: !IPAddress! & echo 子网掩码: !IPSubnet!
- echo 默认网关: !DefaultIPGateway! & echo 主DNS: !DNSServerSearchOrder!
- IF defined dns2 echo DNS2 :!DNS2!
- )
- endlocal
- )
- echo,
- :start
- set/pIndex=输入 适配器 对应 Index 退出 q
- if /i "%Index%" == "q" exit
- if not defined $%Index% goto start
-
- set/ps=输入 t 动态获取IP地址 f 手动获取IP地址
- goto %s%
-
- :t
- :动态获取IP地址
-
- wmic path Win32_NetworkAdapterConfiguration.index=%Index% call enabledhcp
- wmic path Win32_NetworkAdapterConfiguration.index=%Index% call SetDNSServerSearchOrder()
-
- pause & exit
-
- :f
- :手动获取IP地址
-
- set IP="192.168.0.10"
- set Mask="255.255.255.0"
- set Gateway="192.168.0.1"
- set DNS="192.168.0.1"
- wmic nicconfig where index=%Index% call enablestatic(%ip%),(%Mask%)
- wmic nicconfig where index=%Index% call setgateways(%Gateway%)
- wmic nicconfig where index=%Index% call setdnsdomain(%DNS%)
- wmic nicconfig where index=%Index% call SetDNSServerSearchOrder(%DNS%)
-
- pause
复制代码
作者: 34205 时间: 2015-2-23 17:26
回复 1# terse
Windows7下测试通过,获取网络连接名称中,比较完美的方案,楼主知道如何获得网络名称变量吗?
set NetConnectionName_Now=%%j &echo %NetConnectionName_Now%
以上加入07行行不通
作者: snai1 时间: 2021-7-26 12:22
我怎么不能设置呀,只能查看
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |