局域网自动识别机器名和MAC并生成文件的命令

发布于:2025-03-02 ⋅ 阅读:(109) ⋅ 点赞:(0)

更新版本:添加了MAC 地址 确定了设备唯一性

V1.1 局域网自动识别机器名和MAC并生成文件的批处理命令

@echo off
setlocal enabledelayedexpansion

REM 设置输出文件
set outputFile=network_info.txt

REM 清空或创建输出文件
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.%%i
    echo Checking IP: !ip!
    
    REM 使用nbtstat获取机器名和MAC地址
    nbtstat -A !ip! > temp.txt
    
    REM 从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 enabledelayedexpansion

REM 定义输出文件
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%.