返回列表 发帖

[网络工具] 批处理版局域网MAC地址查询

我这里没办法测试A类,B类IP地址,如果有条件的请帮我测试一下,并给一下反馈信息。谢谢
论坛短信也可,QQ也行。
qq:260778312
@echo off
setlocal enabledelayedexpansion
title "局域网MAC地址查询"
echo.
echo.
echo.    在这里,需要注意的是:此批处理仅用于局域网MAC地址查询,输入的IP可以包括任何
echo.IP地址,但是还是需要你有一点点关于IP地址的知识,不要乱输,要分的清楚A、B、C类内
echo.部IP地址含义。而且始终强调的一点是你需要连通主机(能ping通),才能查询的到目标主
echo.机的MAC地址。另外如果你输入的IP地址范围过大,那么扫描的时间就越长,请根据自己的
echo.情况来按标准输入。不要用0开头,比如010.1.1.1或者10.010.1.1等。
echo.    注:IP地址最后一位(0或者255)有特殊用途,IP配置时都不会配置此IP,故扫描时也
echo.        不扫描这两个IP。
echo.                        
echo.                        
echo.                  
pause
cls
echo.
echo.
echo     1. A类IP地址(10.0.0.0-10.255.255.255)
echo.
echo     2. B类IP地址(172.16.0.0-172.31.255.255)
echo.
echo     3. C类IP地址(192.168.0.0-192.168.255.255)
echo.
echo     4. 从IP地址.txt读取IP查询MAC地址
echo.
echo     0. 退出
echo.
echo.
:input
set/p chio=请选择:
if "%chio%" == "0" goto end
if "%chio%" == "1" goto A_
if "%chio%" == "2" goto B_
if "%chio%" == "3" goto C_
if "%chio%" == "4" goto D_
for %%a in ("0 1 2 3 4") do (
  if not "%chio%" == "%%a" (
   echo.
   echo.请选择其中一个输入
   echo.
   goto input
                           )
                            )
:C_
echo.
echo.
set/p min=请输入起始IP:
set/p max=请输入结束IP:
echo.
echo.
for /f "tokens=1,2,3,4,5,6,7,8 delims=." %%a in ("%min%.%max%") do (
  set min_1=%%a
  set min_2=%%b
  set min_3=%%c
  set min_4=%%d
  set max_1=%%e
  set max_2=%%f
  set max_3=%%g
  set max_4=%%h
                                                                    )
if "%min%" == "" cls & echo. 起始IP没有输入,请重新输入 & goto C_
if "%max%" == "" cls & echo. 结束IP没有输入,请重新输入 & goto C_
if %min_1% neq 192 cls & echo.起始IP第一位输入错误 & goto C_
if %min_2% neq 168 cls & echo.起始IP第二位输入错误 & goto C_
if %max_1% neq 192 cls & echo.起始IP第一位输入错误 & goto C_
if %max_2% neq 168 cls & echo.起始IP第二位输入错误 & goto C_
if %min_3% lss 0 cls & echo.起始IP第三位不能小于0 & goto C_
if %min_3% gtr 255 cls & echo.起始IP第三位不能大于255 & goto C_
if %min_4% lss 1 cls & echo.起始IP第四位不能小于1 & goto C_
if %min_4% gtr 254 cls & echo.起始IP第四位不能大于254 & goto C_
if %max_3% lss 0 cls & echo.结束IP第三位不能小于0 & goto C_
if %max_3% gtr 255 cls & echo.结束IP第三位不能大于255 & goto C_
if %max_4% lss 1 cls & echo.结束IP第四位不能小于1 & goto C_
if %max_4% gtr 254 cls & echo.结束IP第四位不能大于254 & goto C_
rem IP地址前三位相同,即是输入的两个IP地址处于同一网段
if %min_1%%min_2%%min_3% equ %max_1%%max_2%%max_3%  (
   if %min_4% lss %max_4%  (
    for /l %%i in (%min_4%,1,%max_4%) do (
     for /f "tokens=1* delims==" %%j in ('ping %min_1%.%min_2%.%min_3%.%%i -n 1 -w 20 /l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %min_1%.%min_2%.%min_3%.%%i') do (
     if not "%%k" == "" echo.arp -s %min_1%.%min_2%.%min_3%.%%i %%k>>arp绑定.bat
                           )
                         )
                                                                           ) else  (
   for /l %%i in (%max_4%,1,%min_4%) do (
     for /f "tokens=1* delims==" %%j in ('ping %min_1%.%min_2%.%min_3%.%%i -n 1 -w 20 /l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %min_1%.%min_2%.%min_3%.%%i') do (
     if not "%%k" == "" echo.arp -s %min_1%.%min_2%.%min_3%.%%i %%k>>arp绑定.bat
                           )
                         )
                       )
                    )
goto :eof
rem ip地址前两位相同,第3位不同,即两个IP地址不处于同一网段
if %min_3% leq %max_3% for /l %%i in (%min_3%,1,%max_3%) do (
  for /l %%j in (1,1,254) do (
    set /a max_5=max_4+1
    if not "%%i.%%j" == "!max_3!.!max_5!" (
    for /f "tokens=1* delims==" %%m in ('ping %min_1%.%min_2%.%%i.%%j -n 1 -w 20 /l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %min_1%.%min_2%.%%i.%%j') do (
     if not "%%n" == "" echo.arp -s %min_1%.%min_2%.%%i.%%j %%n>>arp绑定.bat
                  )
             ) else (
                 goto end
                    )
        )
   )
goto :eof
:B_
echo.
echo.
set/p min=请输入起始IP:
set/p max=请输入结束IP:
echo.
echo.
for /f "tokens=1,2,3,4,5,6,7,8 delims=." %%a in ("%min%.%max%") do (
  set min_1=%%a
  set min_2=%%b
  set min_3=%%c
  set min_4=%%d
  set max_1=%%e
  set max_2=%%f
  set max_3=%%g
  set max_4=%%h
                                                                    )
if "%min%" == "" cls & echo. 起始IP没有输入,请重新输入 & goto B_
if "%max%" == "" cls & echo. 结束IP没有输入,请重新输入 & goto B_
if %min_1% neq 172 cls & echo.起始IP第一位输入错误 & goto B_
if %min_2% lss 16 cls & echo.起始IP第二位不能小于16 & goto B_
if %min_2% gtr 31 cls & echo.起始IP第二位不能大于31 & goto B_
if %max_1% neq 172 cls & echo.结束IP第一位输入错误 & goto B_
if %max_2% lss 16 cls & echo.结束IP第二位不能小于16 & goto B_
if %max_2% gtr 31 cls & echo.结束IP第二位不能大于31 & goto B_
if %min_3% lss 0 cls & echo.起始IP第三位不能小于0 & goto B_
if %min_3% gtr 255 cls & echo.起始IP第三位不能大于255 & goto B_
if %min_4% lss 1 cls & echo.起始IP第四位不能小于1 & goto B_
if %min_4% gtr 254 cls & echo.起始IP第四位不能大于254 & goto B_
if %max_3% lss 0 cls & echo.结束IP第三位不能小于0 & goto B_
if %max_3% gtr 255 cls & echo.结束IP第三位不能大于255 & goto B_
if %max_4% lss 1 cls & echo.结束IP第四位不能小于1 & goto B_
if %max_4% gtr 254 cls & echo.结束IP第四位不能大于254 & goto B_
rem IP地址前三位相同,即是输入的两个IP地址处于同一网段
if %min_1%%min_2%%min_3% equ %max_1%%max_2%%max_3%  (
   if %min_4% lss %max_4%  (
    for /l %%i in (%min_4%,1,%max_4%) do (
     for /f "tokens=1* delims==" %%j in ('ping %min_1%.%min_2%.%min_3%.%%i -n 1 -w 20 /l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %min_1%.%min_2%.%min_3%.%%i') do (
     if not "%%k" == "" echo.arp -s %min_1%.%min_2%.%min_3%.%%i %%k>>arp绑定.bat
                           )
                         )
                                                                           ) else  (
   for /l %%i in (%max_4%,1,%min_4%) do (
     for /f "tokens=1* delims==" %%j in ('ping %min_1%.%min_2%.%min_3%.%%i -n 1 -w 20 /l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %min_1%.%min_2%.%min_3%.%%i') do (
     if not "%%k" == "" echo.arp -s %min_1%.%min_2%.%min_3%.%%i %%k>>arp绑定.bat
                           )
                         )
                       )
                    )
goto :eof
rem ip地址前两位相同,第三位不同,最后一位同不同都没关系
if %min_3% leq %max_3% for /l %%i in (%min_3%,1,%max_3%) do (
  for /l %%j in (1,1,254) do (
    set /a max_5=max_4+1
    if not "%%i.%%j" == "!max_3!.!max_5!" (
    for /f "tokens=1* delims==" %%m in ('ping %min_1%.%min_2%.%%i.%%j -n 1 -w 20 /l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %min_1%.%min_2%.%%i.%%j') do (
     if not "%%n" == "" echo.arp -s %min_1%.%min_2%.%%i.%%j %%n>>arp绑定.bat
                  )
             ) else (
                 goto end
                    )
        )
   )
goto :eof
rem ip地址第二位不同,最后一位同不同都没关系
for /l %%i in (%min_2,1,%max_2%) do (
  for /l %%j in (1,1,254) do (
    for /l %%k in (1,1,254) do (
    set /a max_5=max_4+1
    if not "%%i.%%j.%%k" == "!max_2!.!max_3!.!max_5!" (
    for /f "tokens=1* delims==" %%m in ('ping %min_1%.%%i.%%j.%%k -n 1 -w 20 -l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %min_1%.%%i.%%j.%%k') do (
     if not "%%n" == "" echo.arp -s %min_1%.%%i.%%j.%%k %%n>>arp绑定.bat
   )
                                                      ) else (
                                                          goto end
                                                             )
                               )
                             )
                                     )
goto :eof
:A_
echo.
echo.
set/p min=请输入起始IP:
set/p max=请输入结束IP:
echo.
echo.
for /f "tokens=1,2,3,4,5,6,7,8 delims=." %%a in ("%min%.%max%") do (
  set min_1=%%a
  set min_2=%%b
  set min_3=%%c
  set min_4=%%d
  set max_1=%%e
  set max_2=%%f
  set max_3=%%g
  set max_4=%%h
                                                                    )
if "%min%" == "" cls & echo. 起始IP没有输入,请重新输入 & goto B_
if "%max%" == "" cls & echo. 结束IP没有输入,请重新输入 & goto B_
if %min_1% neq 10 cls & echo.起始IP第一位输入错误 & goto B_
if %min_2% lss 0 cls & echo.起始IP第二位不能小于0 & goto B_
if %min_2% gtr 255 cls & echo.起始IP第二位不能大于255 & goto B_
if %max_1% neq 10 cls & echo.结束IP第一位输入错误 & goto B_
if %max_2% lss 0 cls & echo.结束IP第二位不能小于0 & goto B_
if %max_2% gtr 255 cls & echo.结束IP第二位不能大于255 & goto B_
if %min_3% lss 0 cls & echo.起始IP第三位不能小于0 & goto B_
if %min_3% gtr 255 cls & echo.起始IP第三位不能大于255 & goto B_
if %min_4% lss 1 cls & echo.起始IP第四位不能小于1 & goto B_
if %min_4% gtr 254 cls & echo.起始IP第四位不能大于254 & goto B_
if %max_3% lss 0 cls & echo.结束IP第三位不能小于0 & goto B_
if %max_3% gtr 255 cls & echo.结束IP第三位不能大于255 & goto B_
if %max_4% lss 1 cls & echo.结束IP第四位不能小于1 & goto B_
if %max_4% gtr 254 cls & echo.结束IP第四位不能大于254 & goto B_
rem IP地址前三位相同,即是输入的两个IP地址处于同一网段
if %min_1%%min_2%%min_3% equ %max_1%%max_2%%max_3%  (
   if %min_4% lss %max_4%  (
    for /l %%i in (%min_4%,1,%max_4%) do (
     for /f "tokens=1* delims==" %%j in ('ping %min_1%.%min_2%.%min_3%.%%i -n 1 -w 20 /l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %min_1%.%min_2%.%min_3%.%%i') do (
     if not "%%k" == "" echo.arp -s %min_1%.%min_2%.%min_3%.%%i %%k>>arp绑定.bat
                           )
                         )
                                                                           ) else  (
   for /l %%i in (%max_4%,1,%min_4%) do (
     for /f "tokens=1* delims==" %%j in ('ping %min_1%.%min_2%.%min_3%.%%i -n 1 -w 20 /l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %min_1%.%min_2%.%min_3%.%%i') do (
     if not "%%k" == "" echo.arp -s %min_1%.%min_2%.%min_3%.%%i %%k>>arp绑定.bat
                           )
                         )
                       )
                    )
rem ip地址前两位相同,第三位不同,最后一位同不同都没关系
if %min_3% leq %max_3% for /l %%i in (%min_3%,1,%max_3%) do (
  for /l %%j in (1,1,254) do (
    set /a max_5=max_4+1
    if not "%%i.%%j" == "!max_3!.!max_5!" (
    for /f "tokens=1* delims==" %%m in ('ping %min_1%.%min_2%.%%i.%%j -n 1 -w 20 /l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %min_1%.%min_2%.%%i.%%j') do (
     if not "%%n" == "" echo.arp -s %min_1%.%min_2%.%%i.%%j %%n>>arp绑定.bat
                  )
             ) else (
                 goto end
                    )
        )
   )
rem ip地址第二位不同,最后一位同不同都没关系
for /l %%i in (%min_2,1,%max_2%) do (
  for /l %%j in (1,1,254) do (
    for /l %%k in (1,1,254) do (
    set /a max_5=max_4+1
    if not "%%i.%%j.%%k" == "!max_2!.!max_3!.!max_5!" (
    for /f "tokens=1* delims==" %%m in ('ping %min_1%.%%i.%%j.%%k -n 1 -w 20 -l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %min_1%.%%i.%%j.%%k') do (
     if not "%%n" == "" echo.arp -s %min_1%.%%i.%%j.%%k %%n>>arp绑定.bat
   )
                                                      ) else (
                                                          goto end
                                                             )
                               )
                             )
                                     )
goto :eof
:D_
cls
echo.
echo.
echo         从IP地址.txt读取IP列表,IP地址列表支持格式为x.x.x.x-y.y.y.y的类型。
echo     这里默认两个IP处于同一网段,且前面的IP地址小于后面的IP地址。
echo     支持:
echo             x.x.x.x
echo             y.y.y.y
echo             z.z.z.z
echo             .......
echo     这样的类型,会自动生成hostname ip macaddress格式的文本,顺序跟TXT保持
echo     一致。IP地址.txt需要跟批处理放在同一文件夹下面。暂不支持两种类型混排。
echo.
echo.
pause
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=-" %%a in (IP地址.txt) do (
  if not "%%b" == "" (
    set min=%%a
    set max=%%b      
    call :lp          ) else (
       goto lo
                            )
                                                    )
endlocal
goto end
:lp
for /f "tokens=3,4,8 delims=." %%c in ("!min!.!max!") do (
    set min_2=%%c
    set min_3=%%d
    set min_4=%%e   
                                                         )
for /l %%i in (!min_3!,1,!min_4!) do (
      for /f "skip=8 tokens=1,3,4 delims= " %%j in ('ping 192.168.!min_2!.%%i -n 1 -w 20 /l 1 ^>nul ^& if not errorlevel 1 nbtstat -a 192.168.!min_2!.%%i') do (
        if /i "%%k" == "unique" set hostname=%%j
        if /i "%%j" == "mac"  set mac=%%l &call echo %%hostname%%  192.168.!min_2!.%%i %%mac%% >>MAC备份.txt
                                     )
)
goto :eof
:lo
for /f "delims=" %%c in (IP地址.txt) do (
for /f "skip=8 tokens=1,3,4 delims= " %%j in ('ping %%c -n 1 -w 20 /l 1 ^>nul ^& if not errorlevel 1 nbtstat -a %%c') do (
        if /i "%%k" == "unique" set hostname=%%j
        if /i "%%j" == "mac"  set mac=%%l &call echo %%hostname%%   %%c %%mac%% >>MAC备份.txt
                                     )
)
goto :eof
:end
exitCOPY
努力学习,努力挣分

返回列表