::1.设置环境变量
set MASK=255.255.255.0
set GATEWAY=172.16.1.1
set DNS1=211.137.191.26
set DNS2=219.218.80.66
set WINS=0.0.0.0
set run_log=%temp%\run_log.txt
::3.获取本机所有MAC,地址,格式为 XX-XX-XX-XX-XX-XX
wmic nic get MACAddress >%temp%\name.txt && for /f "skip=1" %%i in ('type %temp%\name.txt') do for /f "tokens=1-6 delims=:" %%I in ("%%i") do echo %%I-%%J-%%K-%%L-%%M-%%N>>%temp%\mac.txt
::4.将获取到的本机MAC地址逐一到本批处理后面的列表去查找,匹配的通常是本地连接的MAC连接,将匹配的MAC地址保存下来。
:: 注1:这种方式可以应对电脑上安装虚拟机,或者机器是win7及以上的系统,会有多个MAC。
:: 注2:至少要有一个匹配MAC,如果找不到任何匹配MAC,则报错退出。这可以避免在没有收集MAC的机器上运行时,错误修改原来的信息。
del %temp%\exist-mac.txt >nul 2>nul
for /f %%i in (%temp%\mac.txt) do find /i "%%i" %0 >nul 2>nul && echo %%i>%temp%\exist-mac.txt
if exist %temp%\exist-mac.txt (for /f %%i in (%temp%\exist-mac.txt) do set MAC=%%i) else (echo ************未找到匹配的MAC项,中断批处理执行************ >>%run_log% &&start %run_log% &&exit)
::5.获取找到MAC地址对应的网络连接名称,通常是本地连接,
:: 注1:对于一些多网卡环境,如机器装有多块网卡,会生成本地连接 2的名称, 注意:2前面有个空格,
:: 注2:还有一种情况,本地连接的名称被改,在学校机房,干这种事情的人大有人在
for /f "tokens=1-6 delims=-" %%i in ("%MAC%") do set MAC1=%%i:%%j:%%k:%%l:%%m:%%n
wmic nic where MACaddress="%MAC1%" get NetConnectionID >%temp%\mac1.txt && for /f "tokens=* skip=1" %%i in ('type %temp%\mac1.txt') do if NOT "%%i"=="" set NetConnectionID=%%i
:intercept
if "%NetConnectionID:~-1%"==" " set "NetConnectionID=%NetConnectionID:~0,-1%"&goto intercept
::6.获取预设的计算机名\IP地址\计算机描述信息
for /f "tokens=1-4" %%i in ('more /e +10 %0 ^|find /i "%mac%"') do set name=%%i&& set IP=%%j&& set desc=%%l
if "%name%"=="" (echo ********************匹配MAC预设计算机信息不存在,本程序将退出******************** >>%run_log% &&start %run_log% &&exit)
if "%ip%"=="" (echo ********************匹配MAC预设IP地址信息不存在,本程序将退出******************** >>%run_log% &&start %run_log% &&exit)
if "%desc%"=="" (echo ********************匹配MAC预设计算机描述信息不存在,本程序将退出****************** >>%run_log% &&start %run_log% &&exit)
for /f "tokens=1-3 skip=2" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\lanmanserver\parameters" /v srvcomment 2^>nul') do set current_desc1=%%k
for /f "tokens=1-3 skip=2" %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters" /v srvcomment 2^>nul') do set current_desc2=%%k
for /f "tokens=1-3 skip=2" %%i in ('reg query "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName" /v ComputerName 2^>nul') do set current_name1=%%k
for /f "tokens=1-4 skip=2" %%i in ('reg query "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Hostname" 2^>nul') do set current_name2=%%l
for /f "tokens=1-3 skip=2" %%i in ('reg query "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v Hostname 2^>nul') do set current_name3=%%k
wmic nicconfig where MACaddress="%MAC1%" get IPaddress>%temp%\a.txt && for /f "skip=1 delims=,{} " %%i in ('type %temp%\a.txt') do set current_ip=%%~i
wmic nicconfig where MACaddress="%MAC1%" get IPSubnet>%temp%\a.txt &&for /f "skip=1 delims=,{} " %%i in ('type %temp%\a.txt') do set IPSubnet=%%~i
wmic nicconfig where MACaddress="%MAC1%" get DefaultIPGateway>%temp%\a.txt &&for /f "skip=1 delims=,{} " %%i in ('type %temp%\a.txt') do set DefaultIPGateway=%%~i
wmic nicconfig where MACaddress="%MAC1%" get DNSServerSearchOrder >%temp%\a.txt && for /f "tokens=1-8 skip=1 delims=,{} " %%i in ('type %temp%\a.txt') do set current_DNS1=%%~i& set current_DNS2=%%~j& set DNS3=%%~k& set DNS4=%%~l
wmic nicconfig where MACaddress="%MAC1%" get WINSPrimaryServer>%temp%\a.txt &&for /f "skip=1 delims=,{} " %%i in ('type %temp%\a.txt') do set WINSPrimaryServer=%%i