Diane
Diane

Reputation: 21

Getting the device name on the local network

In my C# program, I need to get the IP address, mac address and device name on the local network. At the same time, it is necessary to display only those devices whose names begin with tasmota (i.e. I need to find the sonoff relay). I can get IP and mac using the arp -a command, there are no problems with this.

But how to get the device name? nbtstat commands do not work, it says “node was not found”. How can I get the device name? What libraries or commands are there?

Upvotes: 0

Views: 78

Answers (1)

grawity_u1686
grawity_u1686

Reputation: 16562

Short answer: System.Net.Dns.GetHostByAddress()

Longer answer: Where do the names come from in the first place? They aren't inherently part of a network's operation; you should know whether you're dealing with NetBIOS names, mDNS names, names the devices report via DHCP leases (which then become DNS names with help of your router). Some mechanisms support reverse lookups, others do not.

For example, if the device reports its hostname to the router as part of getting a DHCP lease (very common), and the router then provides local DNS service, and the router is smart enough to also provide reverse lookups, then you can use getnameinfo() or the older gethostbyaddr() – I believe the C#.NET equivalent is System.Net.Dns.GetHostByAddress() – to perform a "reverse DNS" lookup. Not all routers implement that fully, though; some only collect the hostnames for display in their own web UI (and of course, the PC might not be using the router for DNS).

Upvotes: 0

Related Questions