更新版本:添加了MAC 地址 確定了設備唯一性
V1.1 局域網自動識別機器名和MAC并生成文件的批處理命令
@echo off
setlocal enabledelayedexpansionREM 設置輸出文件
set outputFile=network_info.txtREM 清空或創建輸出文件
echo Scanning network from 192.168.20.1 to 192.168.20.254... > %outputFile%
echo ========================================== >> %outputFile%REM 循環遍歷IP地址范圍
for /L %%i in (1,1,254) do (set ip=192.168.20.%%iecho Checking IP: !ip!REM 使用nbtstat獲取機器名和MAC地址nbtstat -A !ip! > temp.txtREM 從temp.txt中提取機器名和MAC地址set machineName=set macAddress=for /f "tokens=2 delims= " %%a in ('findstr /i "UNIQUE" temp.txt') do (set machineName=%%a)for /f "tokens=1 delims= " %%a in ('findstr /i "MAC Address" temp.txt') do (set macAddress=%%a)REM 如果找到機器名和MAC地址,則寫入輸出文件if not "!machineName!"=="" (echo IP: !ip! >> %outputFile%echo Machine Name: !machineName! >> %outputFile%echo MAC Address: !macAddress! >> %outputFile%echo -------------------------- >> %outputFile%)REM 刪除臨時文件del temp.txt
)echo Scan completed. Results saved to %outputFile%
pause
V1.0 局域網自動識別機器名并生成文件的批處理命令
@echo off
setlocal enabledelayedexpansionREM 定義輸出文件
set "outputFile=machines.txt"REM 清空或創建輸出文件
echo Scanning network from 192.168.20.1 to 192.168.20.254 > "%outputFile%"REM 循環遍歷IP地址
for /L %%i in (1,1,254) do (set "ip=192.168.20.%%i"echo Checking !ip!...REM 使用nbtstat命令獲取機器名nbtstat -A !ip! >nul 2>&1 && (for /f "tokens=2 delims= " %%a in ('nbtstat -A !ip! ^| find "UNIQUE"') do (echo !ip! - %%a >> "%outputFile%")) || (echo !ip! - No response >> "%outputFile%")
)echo Scan complete. Results saved to %outputFile%.