forked from BornToBeRoot/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ARPCache.ps1
104 lines (85 loc) · 3.39 KB
/
Get-ARPCache.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Get-ARPCache.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Get the ARP cache
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Get the ARP cache
.DESCRIPTION
Get the Address Resolution Protocol (ARP) cache, which is used for resolution of internet layer addresses into link layer addresses.
.EXAMPLE
Get-ARPCache
Interface IPv4Address MACAddress Type
--------- ----------- ---------- ----
192.168.56.1 192.168.56.255 FF-00-00-00-00-FF static
192.168.56.1 224.0.0.22 01-00-5E-00-00-16 static
192.168.56.1 239.255.255.250 01-00-00-00-00-FA static
192.168.178.22 192.168.178.1 5C-00-00-00-00-77 dynamic
192.168.178.22 192.168.178.255 FF-00-00-00-00-FF static
192.168.178.22 224.0.0.22 01-00-00-00-00-16 static
192.168.178.22 239.255.255.250 01-00-00-00-00-FA static
.EXAMPLE
Get-ARPCache | Where-Object {$_.Interface -eq "192.168.178.22"}
Interface IPv4Address MACAddress Type
--------- ----------- ---------- ----
192.168.178.22 192.168.178.1 5C-00-00-00-00-77 dynamic
192.168.178.22 192.168.178.255 FF-00-00-00-00-FF static
192.168.178.22 224.0.0.22 01-00-00-00-00-16 static
192.168.178.22 239.255.255.250 01-00-00-00-00-FA static
.LINK
https://github.com/BornToBeRoot/PowerShell/blob/master/Documentation/Function/Get-ARPCache.README.md
#>
function Get-ARPCache
{
[CmdletBinding()]
param(
)
Begin{
}
Process{
# Regex for IPv4-Address
$RegexIPv4Address = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
$RegexMACAddress = "([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9A-Fa-f]{2}){6}"
# Get the arp cache...
$Arp_Result = arp -a
foreach($line in $Arp_Result)
{
# Detect line where interface starts
if($line -like "*---*")
{
Write-Verbose -Message "Interface $line"
$InterfaceIPv4 = [regex]::Matches($line, $RegexIPv4Address).Value
Write-Verbose -Message "$InterfaceIPv4"
}
elseif($line -match $RegexMACAddress)
{
foreach($split in $line.Split(" "))
{
if($split -match $RegexIPv4Address)
{
$IPv4Address = $split
}
elseif ($split -match $RegexMACAddress)
{
$MACAddress = $split.ToUpper()
}
elseif(-not([String]::IsNullOrEmpty($split)))
{
$Type = $split
}
}
[pscustomobject] @{
Interface = $InterfaceIPv4
IPv4Address = $IPv4Address
MACAddress = $MACAddress
Type = $Type
}
}
}
}
End{
}
}