本帖最后由 aa77dd@163.com 于 2015-8-24 17:54 编辑
[email]http://bbs.bathome.net onmouseov ... ,D%=360)+d;}},33); [/email]
把你的 MAC 地址表填入代码中, 此代码对 MAC 按排列顺序自动编号, 并跳过 网关地址
把 MAC 地址一个一行(首尾都不要有任何其他字符, 不要有任何空白字符)放在 对应的 段标记( :0.x ~~ :3.x )后面;
如果一个有效的 MAC 地址行 和 所对应的 段标记行 之间 有空行, 那么其间的每一个空行都会占用一个 IP 分配;
如果按行位置计算的 IP 分配与网关地址在同一段, 并且行位置 IP 分配 大于或等于 网关地址, 那么这个 IP 分配会加 1, 给网关地址留下分配空间
程序会生成一个 MAC IP 自动分配文件, 列表中的任意 MAC 都会按这个文件分配 IP
要把尾段 0.1/254 1.1/254 2.1/254 3.1/254 都放在一个子网内, 子网掩码可以用 255.255.252.0 (252 二进制为 11111100)
变量 ip_gateway 的初始值 和 子网掩码可修改, 请保持原来的格式- @echo off & setlocal EnableDelayedExpansion
- TITLE %0
- call :initHexTrans
-
- set "ip_gateway=1.1" & rem 设定网关地址后两段, 以比较跳过
- set /a "ind_GW=%ip_gateway:.=*254-1+%"
-
- call :dumpMACList %~s0 MACList MACList.txt & rem 导出 MAC IP 自动分配列表
- start MACList.txt
-
-
- >nul chcp 437 & rem win7 test
-
- set "mac="
- for /f "tokens=12" %%a in ('ipconfig /all ^| find /I "Physical Address"') do set "mac=%%a"
-
- >nul chcp 936 & echo; & rem win7 test
-
- if "%mac%"=="" (
- echo 未成功获取 MAC 地址, 按任意键终止程序
- pause & exit
- )
-
- echo 成功获取 MAC 地址: %mac%
-
- set "matched="
- for /f "delims=:" %%a in ('findstr /n /r /c:"^%mac%" %~s0') do set "ind=%%a" & set "matched=1"
-
- if "%matched%"=="" (
- echo MAC 地址在列表中无匹配, 按任意键终止程序
- pause & exit
- )
- echo 在 %ind% 行匹配到 MAC 地址: %mac%
-
- set /a "ind_SEC0=ind_SEC1=ind_SEC2=ind_SEC3=1<<31^-1"
- REM set ind_SEC
-
- set "SEC_matched=00" & rem 不要改成 0
- for /L %%i in (3 -1 0) do ( rem 不要用正序循环
- for /f "delims=:" %%a in ('findstr /i /n /r /c:"^:%%i.x" %~s0') do (
- set /a "ind_SEC%%i=%%a"
-
- if "!SEC_matched!"=="00" if !ind! gtr !ind_SEC%%i! set /a "ind-=(%%a+1), SEC_matched=%%i"
-
- )
- )
-
- set /a "SEC_matched+=0, ind+=SEC_matched*254"
-
- REM set ind_SEC
- echo 段匹配 SEC_matched : %SEC_matched%
- echo 全局索引 ind : %ind%
-
- echo 网关地址 : !ip_gateway! -- 索引: !ind_GW!
- if "%ip_gateway:~0,1%"=="%SEC_matched%" if %ind% geq %ind_GW% (
- set /a "ind+=1" & rem 跳过网关地址
- echo 需要跳过网关地址
- )
-
- echo 修正全局索引 ind : %ind%
-
- set /a "sec = ind / 254, tail = ind %% 254 + 1"
-
- set /a "name=10000 + sec * 1000 + tail, hexName=name %% 10000, hexH = hexName >> 8 & 0xF, hexM = hexName >> 4 & 0xF, hexL = hexName & 0xF"
-
- set "name=%name:~-4%"
- set "hexName=!$H%hexH%!!$H%hexM%!!$H%hexL%!"
-
- echo 编号十六进制 hexName : %hexName%
-
- set "ip=%sec%.%tail%"
-
- echo 编号 name : %name%
- echo IP : %ip%
-
- echo;netsh interface ip set address name="本地连接" source=static addr=192.168.%ip% mask=255.255.252.0 gateway=192.168.%ip_gateway% 0
- netsh interface ip set address name="本地连接" source=static addr=192.168.%ip% mask=255.255.252.0 gateway=192.168.%ip_gateway% 0
-
-
- call :writeFile %~s0 regFile reg.reg
- regedit /s reg.reg
- del reg.reg
- pause
- exit
-
- :writeFile readfrom label outputFilename
- > %3 cd.
- for /f "delims=" %%a in (%1) do (
- if /i "!getText!"=="Y" if /i "%%a" neq ":end:%2" >> %3 echo %%a
-
- if /i "%%a"==":%2" set "getText=Y"
- if /i "%%a"==":end:%2" set "getText="
- )
- exit /b
-
- :initHexTrans
- for %%a in (0 1 2 3 4 5 6 7 8 9 A B C D E F) do (
- set /a "i=0x%%a"
- set "$H!i!=%%a"
- )
- exit /b
-
- :regFile
- Windows Registry Editor Version 5.00
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName]
- "ComputerName"="!name!"
-
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
- "NV Hostname"="!name!"
- "Hostname"="!name!"
-
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
- "VirtualNetworkNumber"=dword:00000!hexName!
-
- [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NwlnkIpx\Parameters]
- "VirtualNetworkNumber"=dword:00000!hexName!
- :end:regFile
-
-
-
- :dumpMACList readfrom MACListLabel outputFilename
-
- for /f "delims=:" %%a in ('findstr /n /r /c:"^:%2" %1') do set /a "sk=%%a"
-
- > %3 echo MAC NAME HEXNAME IP
- for /f "skip=%sk% delims=" %%M in (%1) do (
-
- if /i "%%M"==":end:%2" exit /b
-
- set "mac=%%M"
-
- if "!mac:~0,1!" neq ":" (
-
- set "matched="
- for /f "delims=:" %%a in ('findstr /n /r /c:"^%%M" %1') do set "ind=%%a" & set "matched=1"
-
- if "!matched!"=="" (
- echo MAC 地址在列表中无匹配, 按任意键终止程序
- pause & exit
- )
-
- set /a "ind_SEC0=ind_SEC1=ind_SEC2=ind_SEC3=1<<31^-1"
-
- set "SEC_matched=00" & rem 不要改成 0
- for /L %%i in (3 -1 0) do ( rem 不要用正序循环
- for /f "delims=:" %%a in ('findstr /i /n /r /c:"^:%%i.x" %1') do (
- set /a "ind_SEC%%i=%%a"
- if "!SEC_matched!"=="00" if !ind! gtr !ind_SEC%%i! set /a "ind-=(%%a+1), SEC_matched=%%i"
- )
- )
-
- set /a "SEC_matched+=0, ind+=SEC_matched*254"
-
- if "!ip_gateway:~0,1!"=="!SEC_matched!" if !ind! geq !ind_GW! (
- set /a "ind+=1" & rem 跳过网关地址
- )
-
- set /a "sec = ind / 254, tail = ind %% 254 + 1"
-
- set /a "name=10000 + sec * 1000 + tail, hexName=name %% 10000, hexH = hexName >> 8 & 0xF, hexM = hexName >> 4 & 0xF, hexL = hexName & 0xF"
-
- set "name=!name:~-4!"
-
- set "hexName="
- for %%a in (!hexH! !hexM! !hexL!) do set "hexName=!hexName!!$H%%a!"
-
- set "ip=!sec!.!tail!"
-
- >> %3 echo !mac! !name! !hexName! !ip!
- )
- )
-
- exit /b
- :end:dumpMACList
-
-
- :MACList
- :0.x
-
- 44-2A-02-74-AA-1C
- D6-FF-94-8D-6F-20
-
- :1.x
- F2-CF-E0-12-0A-6D
- 00-00-00-00-00-00-00-E0
- :2.x
-
- :3.x
- 48-FD-98-43-9F-23
- 2A-90-0E-29-10-AF
-
- :end:MACList
复制代码
|