Last active
July 16, 2023 10:58
-
-
Save i386net/5961be3271e233c756022e9910ac19d9 to your computer and use it in GitHub Desktop.
Scans a specified range of the local network to get hosts online.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-LocalHosts { | |
<# | |
.SYNOPSIS | |
Scans a specified range of the local network. | |
.DESCRIPTION | |
Scans a given range of IP addresses on a local network. Displays hosts that are online | |
and resolves their names, mac addresses and manufacturer. The following databases are used to resolve mac addresses: | |
• maclookup.app | |
• wireshark | |
You must get an API key to use maclookup.app: https://my.maclookup.app/ | |
.PARAMETER IPSubnet | |
Specifies the subnet. | |
.PARAMETER IPRangeStart | |
Specifies the range start. | |
.PARAMETER IPRangeEnd | |
Specifies the range end. | |
.EXAMPLE | |
Get-LocalHosts -IPSubnet 10.10.10 -IPRangeStart 1 -IPRangeEnd 100 | |
.EXAMPLE | |
Get-LocalHosts 10.10.10 1 100 | |
.NOTES | |
Created by DK | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true, HelpMessage="Enter subnet xxx.xxx.xxx")] | |
[string] | |
$IPSubnet, | |
# IP Range Start | |
[Parameter(Mandatory=$true, HelpMessage="Enter range start")] | |
[ValidatePattern('^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$', ErrorMessage="{0} не входит в диапазон от 0 до 255")] | |
# [ValidateRange(0,255)] | |
[int] | |
$IPRangeStart, | |
# IP Range End | |
[Parameter(Mandatory=$true, HelpMessage="Enter range end")] | |
[ValidatePattern('^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$', ErrorMessage="{0} не входит в диапазон от 0 до 255")] | |
# [ValidateRange(0,255)] | |
[int] | |
$IPRangeEnd | |
) | |
begin { | |
$IPs = @() | |
$LocalMachines = @() | |
$apiKey = "YOU API KEY HERE" | |
$MacLookupURL = "https://api.maclookup.app/v2/macs/" | |
$WiresharkURL = "https://gitlab.com/wireshark/wireshark/-/raw/master/manuf" | |
$LocalDbLocation = "$Env:TEMP\db.txt" | |
try { | |
$StatusCode = Invoke-WebRequest -Uri $($MacLookupURL + "000000") | Select-Object -ExpandProperty StatusCode -ErrorAction Stop | |
} | |
catch { | |
Write-Host "Maclookup.app is not reachable" | |
} | |
if (-not (Test-Path -Path $LocalDbLocation)) { | |
try { | |
Invoke-WebRequest -Uri $WiresharkURL | Select-Object -ExpandProperty Content | Out-File -FilePath $LocalDbLocation -ErrorAction Stop | |
Write-Verbose "Offline DB was saved to: $LocalDBLocation" | |
$LocalDB = (Get-Content -Path $LocalDbLocation).Split("`n") | |
} | |
catch { | |
Write-Verbose "Gitlab is unreachable." | |
} | |
} | |
function Get-MacAndCompany { | |
param ( | |
$IP | |
) | |
# $MacAddress = Get-NetNeighbor $IP -ErrorAction SilentlyContinue | Select-Object -ExpandProperty LinkLayerAddress | |
$MacAddress = Get-NetNeighbor $IP -ErrorAction SilentlyContinue | |
if ($MacAddress.State -ne "Unreachable") { | |
$MacAddress = $MacAddress.LinkLayerAddress | |
} else { | |
$MacAddress = $null | |
} | |
if ($MacAddress) { | |
if ($StatusCode -eq 200) { | |
$RequestUrl = $MacLookupURL + $MacAddress + "?apiKey=" + $apiKey | |
try { | |
$Company = Invoke-WebRequest -Uri $RequestUrl | Select-Object -ExpandProperty Content | ConvertFrom-Json | Select-Object -ExpandProperty company -ErrorAction Stop | |
} catch { | |
Write-Verbose "Maclookup is unreachable." | |
} | |
} else { | |
$MacShort = $MacAddress.Substring(0,8) -replace "-",":" | |
$Company = ($LocalDB -match $MacShort -replace "\s+", " " -split " ")[1] | |
} | |
} else { | |
$MacAddress = "No MacAddress" | |
$Company = "No Manufacturer" | |
} | |
return ($MacAddress, $Company) | |
} | |
function Get-HostName { | |
param ( | |
$IP | |
) | |
$HostName = Resolve-DnsName $IP -ErrorAction SilentlyContinue | Select-Object @{n="NameHost"; e={$_.NameHost}} | Select-Object -ExpandProperty NameHost | |
if (-not $HostName) { | |
$HostName = "No Hostname" | |
} | |
return $HostName | |
} | |
} | |
process { | |
($IPRangeStart..$IPRangeEnd) | ForEach-Object { | |
$IP = $IPSubnet + "." + $_ | |
Write-Host $IP | |
$result = Test-Connection $IP -Ping -Count 1 -Quiet | |
if ($result) { | |
Write-Host "+$IP" -ForegroundColor Green | |
$IPs += $IP | |
} | |
} | |
foreach($IP in $IPs) { | |
$MacAddress, $Company = Get-MacAndCompany -IP $IP | |
$HostName = Get-HostName -IP $IP | |
$Machine = [PSCustomObject]@{ | |
IP = $IP | |
MacAddress = $MacAddress | |
Company = $Company | |
HostName = $HostName | |
} | |
$LocalMachines += $Machine | |
} | |
} | |
end { | |
$LocalMachines | Format-Table -Property * -AutoSize | |
$ToDelete = Read-Host "Type [Y] if you would like to delete fiel $LocalDBLocation" | |
if ($ToDelete -eq "Y") { | |
try { | |
Remove-Item -Path $LocalDbLocation -Force -ErrorAction Stop | |
} | |
catch { | |
Write-Host "Can't delete file: $LocalDbLocation" | |
} | |
} else { | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment