Skip to content

Instantly share code, notes, and snippets.

@rdbox rdbox/file.ps1
Last active Jun 5, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.