Creating a csv host import from a network scan
-
Run the following code in powershell (after editing it with your network’s subnets) to create a csv that will import all hosts on your network.
# examples, just gotta put subnets minus the final .x in a string array # Could also be params if this was a function $subnets = @("192.168.1", "192.168.2", "10.2.114", "192.168.0"); $subnets | ForEach-Object { # loop through each subnet for ($i=0; $i -lt 255; $i++) { # loop through 0 to 255 of the subnet $hn = nslookup "$_.$i"; # run nslookup on the current ip in the loop if ($hn[3] -ne $null -AND $hn[3] -ne "") { # does the ip have a dns entry $hostN = $hn[3].Replace("Name:","").Trim(); # parse the nslookup output into a fqdn host name $mac = getMac /S $hostN; # does the hostname have a mac addr. Can also add /U and /P for user and password if not running from a administrative account if ($mac -ne $null) { # was there a mac for the host? $macAddr = $mac[3].Split(' ')[0]; # use the first found mac address and parse it "$hostN,$macAddr" | Out-File C:\hosts.csv -Append -Encoding UTF8; # add the hostname,macaddress to the csv } } } }
-