Forked from pierew/arp-to-ip-lookup-powershell.ps1
Last active
August 8, 2017 13:57
-
-
Save thegooddoctorgonzo/e085931e54c0fc9ff4728a942daf9b91 to your computer and use it in GitHub Desktop.
ARP to IP lookup PowerShell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Gets the IP address of a computer via the MAC address. This will only work on LAN | |
| # segments. By default we'll scan the ARP table, but then defer to an IP scan to | |
| # as needed. | |
| # | |
| # Usage: $returnIpAddress = GetIPFromMAC "12-43-de-52-a9-99" "192.168." | |
| # Pass in the mac address with dashes (-) as the first parameter | |
| # Pass in an IP address with the last number missing, be sure to include the trailing "." | |
| # Be sure to check the value if $returnIpAddress -eq $null to see if a value was actually returned | |
| # | |
| #EDIT:Added functionality to check consecutive network segment numbers 20170727 | |
| function Get-IPFromMAC { | |
| Param ( | |
| [string] $macAddress, | |
| [string] $networkSegment | |
| ) | |
| #will handle mac addresses with normal seperator characters | |
| $macaddress = (($macaddress.Replace(":","-")).Replace(" ","-")).Replace(".","-") | |
| Write-Host "Searching for $macAddress in network segment $networkSegment..." | |
| $ip = arp -a | Select-String $macAddress |% { $_.ToString().Trim().Split(" ")[0] } | |
| if ($ip -eq $null) | |
| { | |
| Write-Host "$macAddress not found in ARP table, deep scan starting..." | |
| for($c = 44; $c -lt 47; $c++)#customize with consecutive net segment numbers | |
| { | |
| for ($i = 1; $i -lt 255; $i++) | |
| { | |
| $ipAddr = $networkSegment + $c + "." + $i | |
| #Write-host "Pinging " $ipAddr | |
| ping "$ipAddr" -n 1 -l 1 -4 -w 90 | Out-Null | |
| $ip = arp -a "$networkSegment$i" | Select-String $macAddress |% { $_.ToString().Trim().Split(" ")[0] } | |
| if ($ip -ne $null) | |
| { | |
| Write-Host "Found $macAddress during deep scan, IP: $ip" | |
| break | |
| } | |
| } | |
| } | |
| } | |
| return $ip | |
| } | |
| #MacAddress Device Rename | |
| Write-Host "Getting Ip-Address from MAC Address" | |
| $ipAddress = GetIPFromMac -macAddress $macAddress -networkSegment $networkSegment | |
| Write-Host "Found IP Address: $ipAddress" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BTW this saved my @$$ today. Having to look up IPs assigned to macs over and over as the IPs are changing after firmware updates. Saved my sanity.