【问题标题】:How to Capture the ARP Table and output to a file in Powershell如何在 Powershell 中捕获 ARP 表并输出到文件
【发布时间】:2017-03-07 17:35:55
【问题描述】:

我在寻找这个问题的答案时遇到了问题。所以我想我会与大众分享我建造的东西。 我试图做的是运行 ARP -a 命令并捕获结果以在 TXT 文件中用于稍后的其他内容。经过大量搜索,我能够编译这个。

我在代码中添加了注释,以帮助那些经验不足的人了解每个部分的作用。

但是这段代码做了这些组件

  1. 设置输出路径(如果需要)
  2. 查找当前机器IP 地址
  3. 将 IP 方案分解为仅托管前 3 个 八位字节。 *如果您使用的是具有多个 NIC 或 IP 地址的机器,则其中有一行代码是 # 以允许您过滤 如果需要,只需要一个。
  4. 捕获 ARP 表。 *我有 2 个版本的 ARP 捕获。第一个是如果您要按设备 IP 方案进行过滤。这将有助于删除 DNS 和环回条目。第二个是如果你想要一切。
  5. 将 IP 地址的结果输出到 txt 文件。

就我而言,我只是用它来过滤 IP 地址。您可能需要它用于其他目的。好消息是,您可以轻松更改这些部分,以便您根据自己的需要进行更多过滤。

    #Captures ARP Table then Displays ALL IPs that match the local computers IP Address

#Set the file you wish for the data to Output to
$OutputPath = "C:\Temp\Test.txt"

#Captures the devices IP Scheme and break it down, remove the Last Octet from the string
#This section is only used IF you are trying to filter the ARP by the local network the device is on.
[string[]]$ComputerName = $env:computername
$OrgSettings = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $ComputerName -EA Stop | ? { $_.IPEnabled }
$ip = $OrgSettings.IPAddress[0]
#IF (!($ip[1] -eq $null)) {$ip = $ip[0]}
$ip = (([ipaddress] $ip).GetAddressBytes()[0..2] -join ".") + "."
$ip = $ip.TrimEnd(".")

#Searches the ARP table for IPs that match the scheme and parses out the data into an Array (It removed the Devices IP from the list.)
Remove-Variable macarray
$macarray = @()
#(arp -a) -match $ip | Foreach{ #Use if needed for filtering results
(arp -a) | Foreach{    #Use this IF no filtering needed
      $obj = New-Object PSObject -Property @{
        IP  = ($_ -split "\s+")[1]
        MAC = ($_ -split "\s+")[2]
      }
     IF (!($obj.MAC -eq "---" -or $obj.MAC -eq "Address" -or $obj.MAC -eq $null -or $obj.MAC -eq "ff-ff-ff-ff-ff-ff")) {$macarray += $obj}
  }

#Outputting the IP Addresses captured.
$macarray | Select -ExpandProperty "IP" | Out-file -FilePath $OutputPath -Force

【问题讨论】:

  • 我在这里看不到您的问题。到底出了什么问题?
  • Windows8/Server2012 或更高版本具有Get-NetNeighbor cmdlet。
  • 这只是您宣传您的代码以供使用吗?如果您为此使用GitHub或其他东西会更合适。

标签: powershell ip mac-address arp


【解决方案1】:

这是我在解析基于文本的表格时使用的东西。当标题与它们下面的数据左对齐时,此代码有效。诀窍是arp 将显示多个接口的数据。如果您需要基于此进行过滤,那么此逻辑将生成一个自定义对象数组,您可以根据需要过滤或输出到文件,现在使用Export-CSV。如果您需要更多关于循环内部发生的事情,它来自another answer of mine.

此方法基于标头字段的位置。没有什么是硬编码的,它都是基于这些索引和字段名称的自定义构建。使用那些$headerIndexes,我们分割每一行并将结果(如果存在)放入其各自的列中。有逻辑可以确保我们不会尝试抓取可能不存在的字符串的任何部分并将最后一个字段视为特殊字段。

$arpResults = arp.exe -a | Out-String
$arpResults | Select-string -Pattern '(?smi)((?<=Interface:).*?(?=Interface:|\Z))' -AllMatches | 
        Select-Object -ExpandProperty Matches | ForEach-Object{
    $interfaceData = $_.Groups[0].Value.Trim() -split "`r`n"

    # Header of the data table is the second array element.
    $headerString = $interfaceData[1]
    $headerElements = $headerString -split "\s{2,}" | Where-Object{$_}
    $headerIndexes = $headerElements | ForEach-Object{$headerString.IndexOf($_)}

    # Skip the first two lines as they are for the interface address and header for each table
    $interfaceData | Select-Object -Skip 2 | ForEach-Object{
        $props = @{Interface = $interfaceData[0].Trim()}
        $line = $_
        For($indexStep = 0; $indexStep -le $headerIndexes.Count - 1; $indexStep++){
            $value = $null            # Assume a null value 
            $valueLength = $headerIndexes[$indexStep + 1] - $headerIndexes[$indexStep]
            $valueStart = $headerIndexes[$indexStep]
            If(($valueLength -gt 0) -and (($valueStart + $valueLength) -lt $line.Length)){
                $value = ($line.Substring($valueStart,$valueLength)).Trim()
            } ElseIf ($valueStart -lt $line.Length){
                $value = ($line.Substring($valueStart)).Trim()
            }
            $props.($headerElements[$indexStep]) = $value    
        }
        [pscustomobject]$props
    } 

} | Select-Object Interface, "Internet Address","Physical Address",Type

样本部分输出

Interface               Internet Address Physical Address  Type   
---------               ---------------- ----------------  ----   
10.10.13.153 --- 0xb    224.0.0.252      01-00-5e-00-00-fc static 
10.10.13.153 --- 0xb    228.5.6.7        01-00-5e-05-06-07 static 
10.10.13.153 --- 0xb    239.255.255.250  01-00-5e-7f-ff-fa static 
10.10.13.153 --- 0xb    255.255.255.255  ff-ff-ff-ff-ff-ff static 
169.254.215.111 --- 0xc 169.254.255.255  ff-ff-ff-ff-ff-ff static 
169.254.215.111 --- 0xc 224.0.0.22       01-00-5e-00-00-16 static 

作为BenH points out in comments ..

Windows8/Server2012 或更高版本具有Get-NetNeighbor cmdlet

【讨论】:

    猜你喜欢
    • 2012-05-10
    • 2010-10-29
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 2018-05-01
    相关资源
    最近更新 更多