我目前整理的代码如下:- @echo off
- mode con cols=70 lines=39
- color e0
- set "NetIndex="
- for /f "tokens=1* delims==" %%a in ('wmic nic where "NetEnabled=TRUE and not Name like '%%Virtual%%'" get index /value^|find "="') do set NetIndex=%%b
- if defined NetIndex (
- echo.当前正在使用的网卡的Index值:%NetIndex%)>nul
- wmic nicconfig where index=%NetIndex% call SetTcpipNetbios 0 >nul
- cls
- setlocal enabledelayedexpansion>nul
- set "MACAddress="
- for /f "tokens=1* delims==" %%a in ('wmic nic where "NetEnabled=TRUE and not Name like '%%Virtual%%'" get MACAddress /value^|find "="') do set MACAddress=%%b
- if defined MACAddress (
- echo.网卡的MAC地址:%MACAddress%)>nul
- )
- set "MACAddress=%MACAddress: =%"
- echo.
- echo. 局域网在线主机扫描助手
- echo.
- echo 输入要扫描的IP段,直接回车则为此前输入的IP段(没输过则为192.168.1):
- set /p IpDuan=
- if "%IpDuan%"=="" (set IpDuan=192.168.1)
- echo 输入要扫描的IP起始位,直接回车则为此前输入的数值(没输过则为2):
- set /p QiShi=
- if "%QiShi%"=="" (set QiShi=2)
- echo 输入要扫描的IP结束位,直接回车则为此前输入的数值(没输过则为255):
- set /p JieShu=
- if "%JieShu%"=="" (set JieShu=255)
- cls
- echo.
- echo. 扫描完1→255的整个网段大约需要3分钟的时间,请耐心等待......
- echo.
- echo. 当前正在扫描[ %IpDuan%.%QiShi%-%IpDuan%.%JieShu% ]IP段内在线的主机。
- ::ping -n 1 -w 99 %ip%/ping -a
- ::nbtstat -a %ip%
- echo.
- echo. 【 局域网在线主机列表 】
- echo. ----------------------------------------------------------------
- echo. ^ IP地址 ^ MAC地址 ^ 主机名 ^
- echo. ----------------------------------------------------------------
- setlocal enableDelayedExpansion
- set "ip3=%IpDuan%"
- for /l %%i in (%QiShi%,1,%JieShu%)do (
- rem echo %%i
- call :CheckIPstatus %ip3%.%%i&&call :GetIPNameMAC %ip3%.%%i Name MAC
- if !errorlevel! equ 0 echo/ [ %ip3%.%%i ] [!mac:~-20! ] [!Name:~-20!]
- )
- echo. ------------------------ 扫描完成 ----------------------------
- echo.
- echo. * 其中MAC地址大写的行是本机信息。
- echo.
- pause |echo 按任意键退出。
- ::[完]
-
- ::ping是否通:返回码stat=失败/Lost
- :CheckIPstatus
- for /f "tokens=3 delims=,," %%p in ('"ping -n 1 -w 99 %1|findstr /i "数据包 Packets""')do (
- for /f "tokens=3" %%s in ("%%p")do set "stat=%%s"
- )
- exit/b %stat%
-
- ::nbtstat查ip对应计算机名、MAC
- :GetIPNameMAC IP Name MAC
- setlocal enabledelayedexpansion
- set name=其他非电脑设备
- set MAC= %MACAddress%
- for /f "tokens=3 delims= " %%m in ('ping -a -n 1 %1 ^|find /i "["') do set "name=%%m "
- echo.name:%name%>nul
- for /f "tokens=2 delims= " %%m in ('arp -a %1 ^|find /i "%1"') do set "MAC= %%m"
- echo.MAC:%MAC%>nul
- if "%MAC%"=="" (set Bcode=1)else set Bcode=0
- endlocal&set %2=%Name%&set %3=%MAC%&exit/b %Bcode%
复制代码 我是白丁,不懂写代码,代码中可能会存在一些无用的或者繁琐的代码,见笑了。
另外,因为扫描整个网段的时间比较长,哪位大神方便帮优化一下,或者加一个进度显示的提示,滚动条或者百分比都行,谢谢! |