Last active
April 30, 2024 19:12
-
-
Save joegasper/93ff8ae44fa8712747d85aa92c2b4c78 to your computer and use it in GitHub Desktop.
Get-PingSweep - super fast (~500ms) subnet ping sweep with option to resolve IP address
This file contains 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
# Inspiration from https://twitter.com/mrhvid/status/929717169130176512 @mrhvid @Lee_Holmes | |
function ResolveIp($IpAddress) { | |
try { | |
(Resolve-DnsName $IpAddress -QuickTimeout -ErrorAction SilentlyContinue).NameHost | |
} catch { | |
$null | |
} | |
} | |
function Get-PingSweep { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$true)] | |
[string]$SubNet, | |
[switch]$ResolveName | |
) | |
$ips = 1..254 | ForEach-Object {"$($SubNet).$_"} | |
$ps = foreach ($ip in $ips) { | |
(New-Object Net.NetworkInformation.Ping).SendPingAsync($ip, 250) | |
#[Net.NetworkInformation.Ping]::New().SendPingAsync($ip, 250) # or if on PowerShell v5 | |
} | |
[Threading.Tasks.Task]::WaitAll($ps) | |
$ps.Result | Where-Object -FilterScript {$_.Status -eq 'Success' -and $_.Address -like "$subnet*"} | | |
Select-Object Address,Status,RoundtripTime -Unique | | |
ForEach-Object { | |
if ($_.Status -eq 'Success') { | |
if (!$ResolveName) { | |
$_ | |
} else { | |
$_ | Select-Object Address, @{Expression={ResolveIp($_.Address)};Label='Name'}, Status, RoundtripTime | |
} | |
} | |
} | |
} | |
<# | |
Get-PingSweep -SubNet '10.56.161' | |
Address Status RoundtripTime | |
------- ------ ------------- | |
10.56.161.1 Success 2 | |
10.56.161.48 Success 0 | |
10.56.161.49 Success 0 | |
10.56.161.51 Success 0 | |
10.56.161.52 Success 0 | |
10.56.161.53 Success 0 | |
10.56.161.54 Success 1 | |
Get-PingSweep -SubNet '10.56.161' -ResolveName | |
Address Name Status RoundtripTime | |
------- ---- ------ ------------- | |
10.56.161.1 b86-bop12-sw18.contoso.com Success 3 | |
10.56.161.48 it-box1-dhwin7.contoso.com Success 1 | |
10.56.161.49 it-box2-dhwin10.contoso.com Success 0 | |
10.56.161.51 it-dept1-uh7yg2.contoso.com Success 0 | |
10.56.161.52 ds101sync.contoso.com Success 0 | |
10.56.161.53 Success 1 | |
10.56.161.54 it-ithd-12tr889.contoso.com Success 1 | |
(Measure-Command -Expression {Get-PingSweep -SubNet '10.56.161' -ResolveName}).Milliseconds | |
441 | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, the script is owesome thanks, but i have some problems.
Every time when i make a scan, different IP addresses are shown, for example:
Each time I am getting different result (list of active devices) somethink like:
Scan 1:
192.168.20.1
192.168.20.6
192.168.20.164
Scan 2:
192.168.20.1
192.168.20.6
192.168.20.31
192.168.20.164
Scan 3:
192.168.20.1
192.168.20.6
192.168.20.107
192.168.20.99
but i know that one device has 192.168.20.100 Address, and then when i manually make a ping 192.168.20.100
and make pingsweep again, then is Address in the list.
Scan 4:
192.168.20.1
192.168.20.6
192.168.20.107
192.168.20.100
My problem is actually, I need to know which devices are online, it seems that pingsweep is super fast but therfore unreliable.
Do you have any advice?
Thanks in advance!