批处理:避免空格和重复条目

时间:2014-05-29 11:51:20

标签: windows batch-file text-processing wmic

我编写了一个批处理脚本,用于扫描网络中的每个IP地址,并获取与该IP地址关联的主机名和MAC地址。

脚本:

@echo off

setlocal enableextensions enabledelayedexpansion

    for /L %%y in (73,1,79) do (
    for /L %%z in (1,1,254) do (

    set "node=172.22.%%y.%%z"

    ping -n 1 !node! | find "TTL=" > NUL
    if not errorlevel 1 (

    for /f "delims=" %%a in (
        'wmic /node:"!node!" computersystem get name /format:csv ^| find /i "!node!"'
    ) do (
    for /f "tokens=1-2 delims=," %%b in ("%%a") do (
        set "_name=%%c"
    ))

    for /f "tokens=1-2 delims==" %%a in ('nbtstat -A "!node!"'
    ) do (

    set "_mac=%%b"
        echo !node!,!_name!,!_mac! >> output.txt

    )
    )
))

扫描输出显示每个IP的多个结果,其中只有一个结果有用且包含MAC地址,而其他结果是重复条目并且在MAC地址处有空格。

输出:

172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 
172.22.73.3,WORKSTATION4, 00-17-7C-11-3A-5F

172.22.73.3,WORKSTATION4, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 
172.22.73.13,WORKSTATION1, 00-14-5E-1E-5A-8E

172.22.73.13,WORKSTATION1, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 
172.22.73.15,MANDAR-PC, 00-1E-0B-9D-DD-E9

然而,理想的输出应该是:

172.22.73.3,WORKSTATION4, 00-17-7C-11-3A-5F
172.22.73.13,WORKSTATION1, 00-14-5E-1E-5A-8E
172.22.73.15,MANDAR-PC, 00-1E-0B-9D-DD-E9

2 个答案:

答案 0 :(得分:0)

替换

echo !node!,!_name!,!_mac! >> output.txt

if not "!_mac!"=="" echo !node!,!_name!,!_mac! >> output.txt

编辑这里是完整的代码,对我有用:

@echo off
setlocal enableextensions enabledelayedexpansion
for /L %%y in (73,1,79) do (
  for /L %%z in (1,1,254) do (
    set "node=172.22.%%y.%%z"
    ping -n 1 !node! | find "TTL=" > NUL
    if not errorlevel 1 (
      for /f "delims=" %%a in ('wmic /node:"!node!" computersystem get name /format:csv ^| find /i "!node!"') do (
        for /f "tokens=1-2 delims=," %%b in ("%%a") do set "_name=%%c"
      )
      for /f "tokens=1-2 delims==" %%a in ('nbtstat -A "!node!"') do (
        set "_mac=%%b"
        if not "!_mac!"=="" echo !node!,!_name!,!_mac: =! >> output.txt
      )
    )
  )
)

答案 1 :(得分:0)

我将代码的最后三行更改为)以上,请查看是否有帮助。

@echo off

setlocal enableextensions enabledelayedexpansion

    for /L %%y in (73,1,79) do (
    for /L %%z in (1,1,254) do (

    set "node=172.22.%%y.%%z"

    ping -n 1 !node! | find "TTL=" > NUL
    if not errorlevel 1 (

    for /f "delims=" %%a in (
        'wmic /node:"!node!" computersystem get name /format:csv ^| find /i "!node!"'
    ) do (
    for /f "tokens=1-2 delims=," %%b in ("%%a") do (
        set "_name=%%c"
    ))

    for /f "tokens=1-2 delims==" %%a in ('nbtstat -A "!node!"'
    ) do (

      set "_mac="
      set "_mac=%%b"
      if defined _mac >> output.txt echo !node!,!_name!,!_mac!

    )
    )
))