织梦CMS - 轻松建站从此开始!

欧博ABG官网-欧博官方网址-会员登入

Use powershell to get device names and their ipadd

时间:2025-12-21 19:01来源: 作者:admin 点击: 7 次
Can find dns with "Resolve-DnsName", and mac/ips with "Get-NetNeighbor" (arp table cache). Can scan ips and use "Test-Connect

Can find dns with "Resolve-DnsName", and mac/ips with "Get-NetNeighbor" (arp table cache). Can scan ips and use "Test-Connection" (ping). "Test-Connection" also lets you look for open tcp ports, so if port 80 is open you can go to that address in the browser, or if port 135 is open can go to that fileshare address. Finally, if you still can't identify the devices you can use a site to lookup the mac address given with arp or use the code at the bottom.

Using the arp table cache to find devices requires powershell 7 or higher: #replace the string with e.g. "192.168.1.$_", whatever your subnet is, optional but will improve # of devices found $ips= 0..255 | %{"10.0.0.$_"}; #optional: add ports to scan. 22=ssh, 80=http, 443=https, 135=smb, 3389=rdp $ports= 22, 80, 443, 135, 3389; #optional: change batch size to speed up / slow down (warning: too high will throw errors) $batchSize=64; $ips += Get-NetNeighbor | %{$_.IPAddress} $ips = $ips | sort | unique; $ips | % -Throttlelimit $batchSize -Parallel { $ip=$_; $activePorts = $using:ports | %{ if(Test-Connection $ip -quiet -TcpPort $_ -TimeoutSeconds 1){ $_ } } if(Test-Connection $ip -quiet -TimeoutSeconds 1 -count 1){ [array]$activePorts+="(ping)"; } if($activePorts){ $dns=(Resolve-DnsName $ip -ErrorAction SilentlyContinue).NameHost; $mac=((Get-NetNeighbor |?{$_.State -ne "Incomplete" -and $_.State -ne "Unreachable" -and $_.IPAddress -match $ip}|%{$_}).LinkLayerAddress ) return [pscustomobject]@{dns=$dns; ip=$ip; ports=$activePorts; mac=$mac} } } | tee -variable "dvcResults" $dvcResults | sort -property mac

note: You can't be sure about dns because a reverse zone may not be configured. (so the dns field may not be perfect)

If you want to lookup the mac address you can use the code below to append this data or go to a website (note: the code below downloads data from standards-oui.ieee.org/oui/oui.txt )

#build dict $oui = (irm standards-oui.ieee.org/oui/oui.txt) -split '\r?\n'; $dict=@{}; $oui | ?{$_ -match "^[^\s]{8}"} | %{$arr=$_ -split "\s+";$dict[$arr[0]]=(($arr |select-object -skip 2) -join " ")} #append data $dvcDescriptions = $dvcResults | %{[pscustomobject]@{dns=$_.dns; ip=$_.ip; ports=$_.ports; companyName=$_.mac ? $dict[$_.mac.Substring(0,8)] : ""; mac=$_.mac;}}; $dvcDescriptions | sort -property companyName | Format-Table

Ex. output:

ex output

Output shows an asus computer hosting a windows fileshare, a dell server hosting https, a raspberry pi with ssh, a mac mini, and an hp printer hosting a webserver. If I go into my router page I can see the raspberry pi device name (pizero1) which our resolve-dnsname wasn't able to figure out.

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-12-23 17:12 最后登录:2025-12-23 17:12
栏目列表
推荐内容