[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 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 的初始值 和 子网掩码可修改, 请保持原来的格式
  1. @echo off & setlocal EnableDelayedExpansion
  2. TITLE %0
  3. call :initHexTrans
  4. set "ip_gateway=1.1" & rem 设定网关地址后两段, 以比较跳过
  5. set /a "ind_GW=%ip_gateway:.=*254-1+%"
  6. call :dumpMACList %~s0 MACList MACList.txt  & rem 导出 MAC IP 自动分配列表
  7. start MACList.txt
  8. >nul chcp 437 & rem win7 test
  9. set "mac="
  10. for /f "tokens=12" %%a in ('ipconfig /all ^| find /I "Physical Address"') do set "mac=%%a"
  11. >nul chcp 936 & echo; & rem win7 test
  12. if "%mac%"=="" (
  13.     echo 未成功获取 MAC 地址, 按任意键终止程序
  14.     pause & exit
  15. )
  16. echo 成功获取 MAC 地址: %mac%
  17. set "matched="
  18. for /f "delims=:" %%a in ('findstr /n /r /c:"^%mac%" %~s0') do set "ind=%%a" & set "matched=1"
  19. if "%matched%"=="" (
  20.     echo MAC 地址在列表中无匹配, 按任意键终止程序
  21.     pause & exit
  22. )
  23. echo 在 %ind% 行匹配到 MAC 地址: %mac%
  24. set /a "ind_SEC0=ind_SEC1=ind_SEC2=ind_SEC3=1<<31^-1"
  25. REM set ind_SEC
  26. set "SEC_matched=00" & rem 不要改成 0
  27. for /L %%i in (3 -1 0) do ( rem 不要用正序循环
  28.     for /f "delims=:" %%a in ('findstr /i /n /r /c:"^:%%i.x" %~s0') do (
  29.         set /a "ind_SEC%%i=%%a"
  30.         if "!SEC_matched!"=="00" if !ind! gtr !ind_SEC%%i! set /a "ind-=(%%a+1), SEC_matched=%%i"
  31.     )
  32. )
  33. set /a "SEC_matched+=0, ind+=SEC_matched*254"
  34. REM set ind_SEC
  35. echo 段匹配   SEC_matched      : %SEC_matched%
  36. echo 全局索引 ind              : %ind%
  37. echo 网关地址                  : !ip_gateway! -- 索引: !ind_GW!
  38. if "%ip_gateway:~0,1%"=="%SEC_matched%" if %ind% geq %ind_GW% (
  39.     set /a "ind+=1" & rem 跳过网关地址
  40.     echo 需要跳过网关地址
  41. )
  42. echo 修正全局索引 ind          : %ind%
  43. set /a "sec = ind / 254, tail = ind %% 254 + 1"
  44. set /a "name=10000 + sec * 1000 + tail, hexName=name %% 10000, hexH = hexName >> 8 & 0xF, hexM = hexName >> 4 & 0xF, hexL = hexName & 0xF"
  45. set "name=%name:~-4%"
  46. set "hexName=!$H%hexH%!!$H%hexM%!!$H%hexL%!"
  47. echo 编号十六进制 hexName      : %hexName%
  48. set "ip=%sec%.%tail%"
  49. echo 编号 name                 : %name%
  50. echo IP                        : %ip%
  51. echo;netsh interface ip set address name="本地连接" source=static addr=192.168.%ip% mask=255.255.252.0 gateway=192.168.%ip_gateway% 0
  52. netsh interface ip set address name="本地连接" source=static addr=192.168.%ip% mask=255.255.252.0 gateway=192.168.%ip_gateway% 0
  53. call :writeFile %~s0 regFile reg.reg
  54. regedit /s reg.reg
  55. del reg.reg
  56. pause
  57. exit
  58. :writeFile readfrom label outputFilename
  59. > %3 cd.
  60. for /f "delims=" %%a in (%1) do (
  61.   if /i "!getText!"=="Y" if /i "%%a" neq ":end:%2" >> %3 echo %%a
  62.   if /i "%%a"==":%2" set "getText=Y"
  63.   if /i "%%a"==":end:%2" set "getText="
  64. )
  65. exit /b
  66. :initHexTrans
  67. for %%a in (0 1 2 3 4 5 6 7 8 9 A B C D E F) do (
  68.     set /a "i=0x%%a"
  69.     set "$H!i!=%%a"
  70. )
  71. exit /b
  72. :regFile
  73. Windows Registry Editor Version 5.00
  74. [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName]
  75. "ComputerName"="!name!"
  76. [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
  77. "NV Hostname"="!name!"
  78. "Hostname"="!name!"
  79. [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
  80. "VirtualNetworkNumber"=dword:00000!hexName!
  81. [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NwlnkIpx\Parameters]
  82. "VirtualNetworkNumber"=dword:00000!hexName!
  83. :end:regFile
  84. :dumpMACList readfrom MACListLabel outputFilename
  85. for /f "delims=:" %%a in ('findstr /n /r /c:"^:%2" %1') do set /a "sk=%%a"
  86. > %3 echo MAC                          NAME    HEXNAME   IP
  87. for /f "skip=%sk% delims=" %%M in (%1) do (
  88.     if /i "%%M"==":end:%2" exit /b
  89.     set "mac=%%M"
  90.     if "!mac:~0,1!" neq ":" (
  91.         set "matched="
  92.         for /f "delims=:" %%a in ('findstr /n /r /c:"^%%M" %1') do set "ind=%%a" & set "matched=1"
  93.         if "!matched!"=="" (
  94.             echo MAC 地址在列表中无匹配, 按任意键终止程序
  95.             pause & exit
  96.         )
  97.         set /a "ind_SEC0=ind_SEC1=ind_SEC2=ind_SEC3=1<<31^-1"
  98.         set "SEC_matched=00" & rem 不要改成 0
  99.         for /L %%i in (3 -1 0) do ( rem 不要用正序循环
  100.             for /f "delims=:" %%a in ('findstr /i /n /r /c:"^:%%i.x" %1') do (
  101.                 set /a "ind_SEC%%i=%%a"
  102.                 if "!SEC_matched!"=="00" if !ind! gtr !ind_SEC%%i! set /a "ind-=(%%a+1), SEC_matched=%%i"
  103.             )
  104.         )
  105.         set /a "SEC_matched+=0, ind+=SEC_matched*254"
  106.         if "!ip_gateway:~0,1!"=="!SEC_matched!" if !ind! geq !ind_GW! (
  107.             set /a "ind+=1" & rem 跳过网关地址
  108.         )
  109.         set /a "sec = ind / 254, tail = ind %% 254 + 1"
  110.         set /a "name=10000 + sec * 1000 + tail, hexName=name %% 10000, hexH = hexName >> 8 & 0xF, hexM = hexName >> 4 & 0xF, hexL = hexName & 0xF"
  111.         set "name=!name:~-4!"
  112.         set "hexName="
  113.         for %%a in (!hexH! !hexM! !hexL!) do set "hexName=!hexName!!$H%%a!"
  114.         set "ip=!sec!.!tail!"
  115.         >> %3 echo !mac!            !name!    !hexName!       !ip!
  116.     )
  117. )
  118. exit /b
  119. :end:dumpMACList
  120. :MACList
  121. :0.x
  122. 44-2A-02-74-AA-1C
  123. D6-FF-94-8D-6F-20
  124. :1.x
  125. F2-CF-E0-12-0A-6D
  126. 00-00-00-00-00-00-00-E0
  127. :2.x
  128. :3.x
  129. 48-FD-98-43-9F-23
  130. 2A-90-0E-29-10-AF
  131. :end:MACList
复制代码

TOP

回复 3# cs8901

2 楼代码及其说明已更新

TOP

回复 5# cs8901
  1. @echo off & setlocal EnableDelayedExpansion
  2. set "mac="
  3. for /f "tokens=12" %%a in ('ipconfig /all ^| find /I "Physical Address"') do set "mac=%%a"
  4. if "%mac%"=="" (
  5.     echo 未成功获取 MAC 地址, 按任意键终止程序
  6.     pause & exit
  7. )
  8. echo 成功获取 MAC 地址: %mac%
  9. set "matched="
  10. for /f "delims=:" %%a in ('findstr /n /r /c:"^%mac%" %~s0') do set "ind=%%a" & set "matched=1"
  11. if "%matched%"=="" (
  12.     echo MAC 地址在列表中无匹配, 按任意键终止程序
  13.     pause & exit
  14. )
  15. echo 在 %ind% 行匹配到 MAC 地址: %mac%
  16. findstr /n /r /c:"^%mac%" %~s0
  17. for /f "tokens=1-4" %%a in ('findstr /n /r /c:"^%mac%" %~s0') do (
  18.     echo;netsh interface ip set address name="本地连接" source=static addr=192.168.%%d.%%c mask=255.255.255.0 gateway=192.168.1.1 0
  19.     netsh interface ip set address name="本地连接" source=static addr=192.168.%%d.%%c mask=255.255.255.0 gateway=192.168.1.1 0
  20.     set "name=%%b"
  21.     set "hexName=00000000%%b" & set "hexName=!hexName:~-8!"
  22. )
  23. call :writeFile %~s0 regFile reg.reg
  24. regedit /s reg.reg
  25. del reg.reg
  26. pause
  27. exit
  28. :writeFile readfrom label outputFilename
  29. > %3 cd.
  30. for /f "delims=" %%a in (%1) do (
  31.   if /i "!getText!"=="Y" if /i "%%a" neq ":end:%2" >> %3 echo %%a
  32.   if /i "%%a"==":%2" set "getText=Y"
  33.   if /i "%%a"==":end:%2" set "getText="
  34. )
  35. exit /b
  36. :regFile
  37. Windows Registry Editor Version 5.00
  38. [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName]
  39. "ComputerName"="!name!"
  40. [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
  41. "NV Hostname"="!name!"
  42. "Hostname"="!name!"
  43. [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
  44. "VirtualNetworkNumber"=dword:!hexName!
  45. [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NwlnkIpx\Parameters]
  46. "VirtualNetworkNumber"=dword:!hexName!
  47. :end:regFile
  48. :MACList
  49. [ComputersMAC]       [Name] [ip] [sec]
  50. 44-2A-02-74-AA-1C    001    1    0
  51. 2A-90-0E-29-10-AF    004    4    1
  52. F2-CF-E0-12-0A-6D    062    95    1
  53. D6-FF-94-8D-6F-20    002    2    2
  54. 48-FD-98-43-9F-23    003    3    3
  55. :end:MACList
复制代码

TOP

返回列表