Skip to content

Instantly share code, notes, and snippets.

@rdbox
Last active June 5, 2017 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdbox/11b4cefd2ca04bc8b19d22f4218d000f to your computer and use it in GitHub Desktop.
Save rdbox/11b4cefd2ca04bc8b19d22f4218d000f to your computer and use it in GitHub Desktop.
Check ip-range (powershell)
# This script is written on powershell
# Allows you to quickly check the range of ip addresses
# 2 parameters are used
# (threads and address)
# Example:
# .\file.ps1 -Address (1..20|%{"192.168.1.$_"}) -threads 5
# Will do a check in 5 threads, from 0 to 20 ip
#
# Example:
# .\file.ps1 -Address (1..254|%{"192.168.1.$_"}) -threads 20 |
# where {$_.status -eq "success"}
# Example:
# $IPs = 1..3 | %{$s=$_; 1..254 | %{"192.168.$s.$_"}}
# .\file.ps1 -Address $Ips -threads 20 |
# where {$_.status -eq "success"} |
# Select -expandproperty Address
Param (
[string[]]$Address = $(1..20 | %{"192.168.1.$_"}),
[int]$Threads = 5
)
write-host "Distributing addresses around jobs"
$JobAddresses = @{}
$CurJob = 0
$CurAddress = 0
while ($CurAddress -lt $Address.count)
{
$JobAddresses[$CurJob] += @($Address[$CurAddress])
$CurAddress++
if ($CurJob -eq $Threads -1)
{
$CurJob = 0
}
else
{
$CurJob++
}
}
$Jobs = @()
foreach ($n in 0 .. ($Threads-1))
{
Write-host "Starting job $n, for addresses $($JobAddresses[$n])"
$Jobs += Start-Job -ArgumentList $JobAddresses[$n] -ScriptBlock {
$ping = new-object System.Net.NetworkInformation.Ping
Foreach ($Ip in $Args)
{
trap {
new-object psobject -Property {
Status = "Error: $_"
Address = $Ip
RoundtripTime = 0
}
Continue
}
$ping.send($Ip,100) | select `
@{name="Status"; expression={$_.Status.ToString()}},
@{name = "Address"; expression={$Ip}}, RoundtripTime
}
}
}
write-host "Waiting for jobs"
$ReceivedJobs = 0
while ($ReceivedJobs -le $Jobs.Count)
{
foreach ($CompletedJob in ($Jobs | where {$_.State -eq "Completed"}))
{
Receive-Job $CompletedJob | select status, address, roundtriptime
$ReceivedJobs ++
sleep 1
}
}
Remove-Job $Jobs
write-host "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment