Code: Select all
FOR /L %i IN (1,1,254) DO ping -n 1 -w 100 192.168.1.%i | find "Risposta da" >> risultati.txt
2 - With Powershell
Code: Select all
1..254 | ForEach-Object {Test-Connection -ComputerName "192.168.1.$_" -Count 1 -Quiet} | Where-Object {$_ -eq $true} | ForEach-Object {"Host attivo: 192.168.1.$_"}
2 - With a Powershell script that also resolves names
Scan the LAN of the active network interface:
.\Scan-NetworkAndResolve.ps1
Scan a different LAN:
.\Scan-NetworkAndResolve.ps1 -networkPrefix "192.168.1"
Scan a custom range:
.\Scan-NetworkAndResolve.ps1 -startRange 10 -endRange 50
Scan and save results to a file:
.\Scan-NetworkAndResolve.ps1 -saveToFile
Code: Select all
param(
[Parameter(Mandatory=$false)]
[string]$networkPrefix,
[Parameter(Mandatory=$false)]
[int]$startRange = 1,
[Parameter(Mandatory=$false)]
[int]$endRange = 254,
[Parameter(Mandatory=$false)]
[string]$outputPath = "network_scan_results.csv",
[Parameter(Mandatory=$false)]
[switch]$saveToFile
)
function Get-DefaultNetwork {
try {
# Ottieni l'interfaccia con il default gateway
$defaultAdapter = Get-NetRoute -DestinationPrefix "0.0.0.0/0" |
Get-NetAdapter |
Where-Object Status -eq "Up" |
Select-Object -First 1
if ($defaultAdapter) {
# Ottieni la configurazione IP dell'interfaccia
$ipConfig = Get-NetIPConfiguration -InterfaceIndex $defaultAdapter.ifIndex
$ipAddress = $ipConfig.IPv4Address.IPAddress
$subnetMask = $ipConfig.IPv4Address.PrefixLength
if ($ipAddress) {
# Calcola il network prefix
$octets = $ipAddress.Split('.')
$networkPrefix = "$($octets[0]).$($octets[1]).$($octets[2])"
Write-Host "Rilevata rete attiva: $networkPrefix.0/$subnetMask" -ForegroundColor Cyan
Write-Host "Interfaccia: $($defaultAdapter.Name)" -ForegroundColor Cyan
Write-Host "Indirizzo IP: $ipAddress" -ForegroundColor Cyan
return $networkPrefix
}
}
throw "Nessuna interfaccia di rete attiva trovata con default gateway"
}
catch {
Write-Host "Errore nel rilevamento della rete: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
}
try {
# Se non specificato, rileva automaticamente il network prefix
if (-not $networkPrefix) {
$networkPrefix = Get-DefaultNetwork
}
Write-Host "`nInizio scansione della rete $networkPrefix.[$startRange-$endRange]`n" -ForegroundColor Cyan
# Array per memorizzare i risultati
$results = @()
# Funzione per fare il ping con timeout ridotto
function Test-ConnectionQuick {
param([string]$ip)
$ping = New-Object System.Net.NetworkInformation.Ping
try {
$reply = $ping.Send($ip, 100) # timeout di 100ms
return $reply.Status -eq 'Success'
}
catch {
return $false
}
}
# Scansione della rete
for ($i = $startRange; $i -le $endRange; $i++) {
$ip = "$networkPrefix.$i"
Write-Progress -Activity "Scansione rete" -Status "Test IP: $ip" -PercentComplete (($i-$startRange)/($endRange-$startRange)*100)
if (Test-ConnectionQuick -ip $ip) {
Write-Host "Dispositivo trovato: $ip" -ForegroundColor Green
# Risoluzione DNS
try {
$dnsResult = [System.Net.Dns]::GetHostEntry($ip)
$hostname = $dnsResult.HostName
Write-Host "`t$hostname" -ForegroundColor Green
}
catch [System.Net.Sockets.SocketException] {
Write-Host "`tNessun nome DNS associato" -ForegroundColor Yellow
$hostname = "No DNS"
}
catch {
Write-Host "`tErrore risoluzione DNS: $($_.Exception.Message)" -ForegroundColor Red
$hostname = "Errore risoluzione"
}
# Aggiungi al array dei risultati
$results += [PSCustomObject]@{
IP = $ip
Hostname = $hostname
Status = "Online"
}
}
}
Write-Host "`nScansione completata. Trovati $($results.Count) dispositivi attivi." -ForegroundColor Cyan
# Mostra risultati in formato tabella
$results | Format-Table -AutoSize
# Se richiesto, salva su file
if ($saveToFile) {
$results | Export-Csv -Path $outputPath -NoTypeInformation
Write-Host "`nRisultati salvati in $outputPath" -ForegroundColor Green
}
}
catch {
Write-Host "Errore durante l'esecuzione dello script: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}