We were looking through some old scripts in our library and found the following which you might use to see if a device is actually using the IP’s address you have assigned to them (i.e. exclusions or reservations).

Keep in mind that this is a simple ping test and that many things will not respond to pings. For instance, firewalls often block ping, and some devices like phones and watches do not usually respond to pings by default.


ipscan

# --- Main Script ---

$startIp = Read-Host "Enter the starting IP address (e.g., 192.168.1.10)"
$endIp = Read-Host "Enter the ending IP address (e.g., 192.168.1.50)"

# Split IPs into octets
$startOctets = $startIp.Split('.')
$endOctets = $endIp.Split('.')

# Basic validation: Ensure 4 octets and valid numbers
if ($startOctets.Count -ne 4 -or $endOctets.Count -ne 4) {
    Write-Error "Invalid IP address format. Please enter a valid IPv4 address (e.g., 192.168.1.10)."
    exit 1
}
if (-not ($startOctets[0..2] | ForEach-Object { $_ -match '^\d{1,3}$' -and [int]$_ -ge 0 -and [int]$_ -le 255 }) -or
    -not ($endOctets[0..2] | ForEach-Object { $_ -match '^\d{1,3}$' -and [int]$_ -ge 0 -and [int]$_ -le 255 })) {
    Write-Error "Invalid octet value. IP address octets must be between 0 and 255."
    exit 1
}

# Convert last octets to integers
$startLastOctet = [int]$startOctets[3]
$endLastOctet = [int]$endOctets[3]

# Extract the common prefix (first three octets)
$ipPrefix = "$($startOctets[0]).$($startOctets[1]).$($startOctets[2])"

# Validate that the first three octets match (crucial for this simplified version)
if ("$($endOctets[0]).$($endOctets[1]).$($endOctets[2])" -ne $ipPrefix) {
    Write-Error "ERROR: The starting and ending IP addresses must be in the same /24 subnet (i.e., the first three octets must match). This simplified script does not support scanning across different subnets."
    exit 1
}

# Ensure start last octet is not greater than end last octet
if ($startLastOctet -gt $endLastOctet) {
    Write-Error "The last octet of the starting IP ($startLastOctet) cannot be greater than the last octet of the ending IP ($endLastOctet)."
    exit 1
}

Write-Host "`nPinging IPs from $($startIp) to $($endIp)..."
Write-Host "----------------------------------------------------"

# Iterate through the last octet
for ($i = $startLastOctet; $i -le $endLastOctet; $i++) {
    $currentIp = "$($ipPrefix).$($i)"
    Write-Host "Pinging $($currentIp)..." -NoNewline

    # Test-Connection pings the IP address
    # -Count 1 sends a single ping packet
    # -ErrorAction SilentlyContinue suppresses errors for unreachable hosts
    # -Quiet returns a boolean (True if successful, False if not)
    $pingResult = Test-Connection -ComputerName $currentIp -Count 1 -ErrorAction SilentlyContinue -Quiet

    if ($pingResult) {
        Write-Host " Response received." -ForegroundColor Green
    } else {
        Write-Host " No response." -ForegroundColor Red
    }
}


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *