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

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

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

bash 复制代码
@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%.
相关推荐
YuMiao10 分钟前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux
Sinclair3 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux