IpRangeScan

Here’s a script to scan an entire IP range. It allows to find out what IP addresses are free and what are used and displays some basics informations for the used IPs (hostname, mac address, response time); then, the script generates an HTML report with the results.

<#
.SYNOPSIS
    IpRangeScan - Scans an IP Range.
.DESCRIPTION
    IpRangeScan - Scans an IP Range and generate a report with some informations (hostname, mac address, response time) if the IP address is used
.PARAMETER Range
    Defines the IP address range to scan.
.NOTES
    File Name  : IpRangeScan.ps1
    Author     : Fabrice ZERROUKI - fabricezerrouki@hotmail.com
.EXAMPLE
    PS D:\>IpRangeScan.ps1 -range 192.168.1
    The script will scan all IP addresses from 192.168.1.1 to 192.168.1.254
#>
Param(
    [Parameter(Mandatory=$true, HelpMessage="You must provide a IP addresses range to be scanned.")]
	[ValidatePattern('^\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?){1}\b$')]
	[string]$range
    )

$results=@()
$report="$env:TEMP" + "\IpRangeScan.hta"
#Delete previous results file if exists
if ((Get-Item $report -ErrorAction "SilentlyContinue").Exists) {Write-Host "Deleting old $report`n" -ForegroundColor Yellow; Remove-Item $report}
$ScanDate=Get-Date -Format "dd/MM/yyyy HH:mm:ss"
$global:ErrorActionPreference="SilentlyContinue"
Write-Host "`nRunning the IP address scan for the range $range.xxx`n" -ForegroundColor Yellow

function Get-Mac($line)
{
$macRegex=[regex] "([0-9a-fA-F]{2}-){5}([0-9a-fA-F]{2})"
$macRegex.match($line)
}
function Get-Ping($line)
{
$pingRegex=[regex] "temps=\d+ ms"
$pingRegex.match($line)
}

1..254 | ForEach {
if (!(Test-Connection -Count 1 -ComputerName "$range.$_" -Quiet))
{
Write-Host "$range.$_ is Free" -ForegroundColor DarkGreen
$status = @{
  'Hostname'="N/A";
  'MAC Address'="N/A";
  'Response Time'="N/A";
  'IP Address'="$range.$_";
  'Availability'="<span style='color: green;'>Free</span>";
  }
 $obj=New-Object -TypeName PSObject -Property $status
 $results+=$obj
}
else {
Write-Host "$range.$_ is Used" -ForegroundColor DarkRed
$IpAddress="$range" + "." + "$_"
$computername=[System.Net.Dns]::GetHostbyAddress($IpAddress).HostName
if ($computername -eq $null) {$computername = "unable to get the hostname"}

$arp=cmd /c "arp -a $IpAddress"
$mac=Get-Mac $arp | Select-Object -ExpandProperty Value
$ping=cmd /c "ping $IpAddress -n 1"
$reply=get-ping $ping | Select-Object -ExpandProperty Value
$reply=$reply -replace "temps=", ""

$status = @{
  'Hostname'="$computername";
  'MAC Address'="$mac";
  'Response Time'="$reply";
  'IP Address'="$range.$_";
  'Availability'="<span style='color: orangered;'>Used</span>";
  }
 $obj=New-Object -TypeName PSObject -Property $status
 $results+=$obj
}
}

$style=@"
	<title>IP Range Scan Results</title> 
	<style type="text/css">
	h1 {margin: 0; padding: 0; position: relative; background: #104E8B;color: #FFF;}
	body {font: 12px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;}
	table{border: 1px dotted #CCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px; width:100%;}
	th{background-color: #104E8B; color: #FFF; font-weight: bold; text-align:left; vertical-align:top;}
	td{border: 1px dotted #CCC; text-align:left; vertical-align:top;}
	#page-wrap { width: 50%; margin: 100px auto; }
	#bottom {clear:both; text-align:right;}
	</style>
"@
$body=@"
	<h1>&nbsp;IP Range Scan Results</h1><br />
	<ul>
	<li>Scan Date: $ScanDate</li>
	<li>IP Range scanned: $Range.xxx</li>
	</ul>
"@
$bottom=@"
	<div id="bottom">&copy; IpRangeScan.ps1 - 2012</div>
"@
	
Write-Host "`nGenerating the report`n" -ForegroundColor Yellow
$results | ConvertTo-HTML -Head $style -Body $body -PostContent $bottom  | % {$tmp = $_ -replace "&lt;","<"; $tmp -replace "&gt;",">";} |Out-File $report
Start-Sleep 2
Invoke-Expression $report

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top