PowerShell: ping an arbitrary IP subnet

This will ping an arbitrary subnet; feed it an ip range + subnet mask and it will ping all the hosts. Written primarily to handle “hidden” SAN IPs that wouldn’t show up on DHCP.
=======================
Clear-Host #clears the screen, just like CLS does in MS-DOS

#initialises counters to keep track of the various subnet octets. Octet 3 (4th octet) has to be at least 1, so this gets set to 1 regardelss of user input
[int]$counterOctet0 = 0
[int]$counterOctet1 = 0
[int]$counterOctet2 = 0
[int]$counterOctet3 = 1

[int]$singleHostOctet0 = 255
[int]$singleHostOctet1 = 255
[int]$singleHostOctet2 = 255
[int]$singleHostOctet3 = 255

[int]$customIPRange0 = Read-Host -Prompt “Please enter the 1st IP octet”
[int]$customIPRange1 = Read-Host -Prompt “Please enter the 1st IP octet”
[int]$customIPRange2 = Read-Host -Prompt “Please enter the 1st IP octet”
[int]$customIPRange3 = Read-Host -Prompt “Please enter the 1st IP octet”

[int]$customSubnetOctet0 = Read-Host -Prompt “Please enter the 1st subnet octet”
[int]$customSubnetOctet1 = Read-Host -Prompt “Please enter the 2nd subnet octet”
[int]$customSubnetOctet2 = Read-Host -Prompt “Please enter the 3rd subnet octet”
[int]$customSubnetOctet3 = Read-Host -Prompt “Please enter the 4th subnet octet”

$customIPRange3 = $counterOctet3

[System.Net.IPAddress]$ipAddress = “$customIPRange0.$customIPRange1.$customIPRange2.$customIPRange3”

$resultOctet0 = $singleHostOctet0 – $customSubnetOctet0
$resultOctet1 = $singleHostOctet1 – $customSubnetOctet1
$resultOctet2 = $singleHostOctet2 – $customSubnetOctet2
$resultOctet3 = $singleHostOctet3 – $customSubnetOctet3

If($resultOctet0 -ne 0)
{
If($resultOctet1 -ne 0)
{
If($resultOctet2 -ne 0)
{
If($resultOctet3 -ne 0)
{
Do
{
Do
{
Do
{
Do
{
$result0 = Test-Connection $ipAddress.IPAddressToString -Quiet
If($result0 -eq $true)
{
Try
{
$hostName = [System.Net.Dns]::GetHostEntry(“$ipAddress”).HostName
}
Catch [system.exception]
{
$hostName = “no host name”
}
Write-Host “The IP Address $ipAddress ($hostName) is responding”
}
ElseIf($result0 -eq $false)
{
Write-Host “The IP Address $ipAddress is not responding”
}
$customIPRange3 = $customIPRange3 + 1
$ipAddress = “$customIPRange0.$customIPRange1.$customIPRange2.$customIPRange3”
}
While ($customIPRange3 -lt 255)

$customIPRange3 = $counterOctet3
$customIPRange2 = $customIPRange2 + 1
[System.Net.IPAddress]$ipAddress = “$customIPRange0.$customIPRange1.$customIPRange2.$customIPRange3”
}
While ($customIPRange2 -lt 255)

$customIPRange3 = $counterOctet3
$customIPRange2 = $counterOctet2
$customIPRange1 = $customIPRange1 + 1
[System.Net.IPAddress]$ipAddress = “$customIPRange0.$customIPRange1.$customIPRange2.$customIPRange3”
}
While ($customIPRange1 -lt 255)

$customIPRange3 = $counterOctet3
$customIPRange2 = $counterOctet2
$customIPRange1 = $counterOctet1
$customIPRange0 = $customIPRange0 + 1
[System.Net.IPAddress]$ipAddress = “$customIPRange0.$customIPRange1.$customIPRange2.$customIPRange3”
}
While ($customIPRange0 -lt 255)
}
Else
{
}
}
Else
{
}
}
Else
{
}
}
ElseIf($resultOctet1 -ne 0)
{
If($resultOctet2 -ne 0)
{
If($resultOctet3 -ne 0)
{
Do
{
Do
{
Do
{
$result1 = Test-Connection $ipAddress.IPAddressToString -Quiet
If($result1 -eq $true)
{
Try
{
$hostName = [System.Net.Dns]::GetHostEntry(“$ipAddress”).HostName
}
Catch [system.exception]
{
$hostName = “no host name”
}
Write-Host “The IP Address $ipAddress ($hostName) is responding”
}
ElseIf($result1 -eq $false)
{
Write-Host “The IP Address $ipAddress is not responding”
}
$customIPRange3 = $customIPRange3 + 1
$ipAddress = “$customIPRange0.$customIPRange1.$customIPRange2.$customIPRange3”
}
While ($customIPRange3 -lt 255)

$customIPRange3 = $counterOctet3
$customIPRange2 = $customIPRange2 + 1
[System.Net.IPAddress]$ipAddress = “$customIPRange0.$customIPRange1.$customIPRange2.$customIPRange3”
}
While ($customIPRange2 -lt 255)

$customIPRange3 = $counterOctet3
$customIPRange2 = $counterOctet2
$customIPRange1 = $customIPRange1 + 1
[System.Net.IPAddress]$ipAddress = “$customIPRange0.$customIPRange1.$customIPRange2.$customIPRange3”
}
While ($customIPRange1 -lt 255)
}
Else
{
}
}
Else
{
}
}
ElseIf($resultOctet2 -ne 0)
{
If($resultOctet3 -ne 0)
{
Do
{
Do
{
$result2 = Test-Connection $ipAddress.IPAddressToString -Quiet
If($result2 -eq $true)
{
Try
{
$hostName = [System.Net.Dns]::GetHostEntry(“$ipAddress”).HostName
}
Catch [system.exception]
{
$hostName = “no host name”
}
Write-Host “The IP Address $ipAddress ($hostName) is responding”
}
ElseIf($result2 -eq $false)
{
Write-Host “The IP Address $ipAddress is not responding”
}
$customIPRange3 = $customIPRange3 + 1
$ipAddress = “$customIPRange0.$customIPRange1.$customIPRange2.$customIPRange3”
}
While ($customIPRange3 -lt 255)

$customIPRange3 = $counterOctet3
$customIPRange2 = $customIPRange2 + 1
[System.Net.IPAddress]$ipAddress = “$customIPRange0.$customIPRange1.$customIPRange2.$customIPRange3”
}
While ($customIPRange2 -lt 255)
}
Else
{
}
}
ElseIf($resultOctet3 -ne 0)
{
Do
{
$result3 = Test-Connection $ipAddress.IPAddressToString -Quiet
If($result3 -eq $true)
{
Try
{
$hostName = [System.Net.Dns]::GetHostEntry(“$ipAddress”).HostName
}
Catch [system.exception]
{
$hostName = “no host name”
}
Write-Host “The IP Address $ipAddress ($hostName) is responding”
}
ElseIf($result3 -eq $false)
{
Write-Host “The IP Address $ipAddress is not responding”
}
$customIPRange3 = $customIPRange3 + 1
$ipAddress = “$customIPRange0.$customIPRange1.$customIPRange2.$customIPRange3”
}
While ($customIPRange3 -lt 255)
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s