Writing a Python program for my PiZeroW to scan and inventory my home local, internal network. I can easily iterate though the 256 internal DHCP available IP addresses (some assigned, some not), but cannot figure out what library utility to use to get hostname, given an IP address (reverse DNS).

I know how to do this for IP's that are Internet-addressable, but here I am working with my local, internal, non-Internet-addressable IP's. When I use PING on an internal hostname it returns the internal IP address, so somehow PING is able to look this up on my DHCP server. Looking for recommendations how to do this with a utility in the shell, or better inside a Python program. Thanks!

  • I know how to do this for IP's that are Internet-addressable - show how you do that – Jaromanda X Aug 2 '18 at 0:09

I don't believe there is a general way to do this. Reverse DNS lookups in the public IP address space of the Internet are supported by a DNS infrastructure that includes PTR records, and the .in-addr.arpa domain that's dedicated to reverse lookups. None of that is available on a LAN that uses private IP addresses.

This is not to say that you can't do reverse lookups on your LAN, but if and how to do it depend on how your LAN is configured, and what services are available.

But please don't take this as an attempt to discourage you from your plan. Consider the positives of undertaking such a project: You'll learn a tremendous amount (you'll have to in this case :), and if you're successful, you've created a legacy that may benefit others in ways you can't even imagine now. If this is something that draws your interest, consider breaking it down into smaller problems first, and attacking those. Research on these keywords or tags may be a good start: dns, dhcp, dhcpcd, dnsmasq, zero configuration networking, avahi, bonjour, and on and on. Read to understand the design, and study code written by others. And of course, ask lots of questions :)

But before you begin your research, you should read this poem.

Hope this helps.


  • Since I hoped to write my Python program to do network inventory scanning for any (most) home LAN networks, if there is wide variation on how those LAN networks are configured, then I guess I am out of luck... – KennyM Aug 2 '18 at 1:17
  • @KennyM: Please don't take what I've said as discouragement. I was expounding on that here in this comment, but ran out of space - so I edited my answer instead. Please read it again. – Seamus Aug 3 '18 at 13:03

Some comments: It's not that ping is going against the DHCP. DHCP service provided a dns server when setting up each host that requested network configuration (the dns server could be the DHCP-provider itself but it's not mandatory... it's a completely separate service from DHCP). Then, what ping does is actually ask the name resolution service of machine where it runs... name resolution can depend on a number of items, one of them being a dns service, but also names can be set up "statically" on files... like on /etc/hosts on UNIX and Linux boxes and stuff (same thing is provided on Windows but I don't know the file name). Then, if you are talking about doing the exact opposite of what DNS normally does (map hostnames to IP addresses), then what you want to do is a "reverse" name resolution request against DNS. As long as the reverse zone is set up correctly on the dns service, it should work. Using dig: dig -x some-ip-address. That should do.

  • Sorry, yes, I did know and should have noted that DNS is separate from DHCP. Of course this is "local DNS", not my ISP's Internet DNS service that my firewall proxy's requests to. I did try "dig" with -x for reverse lookups but it is not returning a hostname I know to be assigned to a given internal IP address. – KennyM Aug 2 '18 at 1:22
  • See dig "at" -x ; <<>> DiG 9.10.3-P4-Raspbian <<>> @ -x ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 37770 ;; flags: qr rd ra ad; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ; IN PTR ;; Query time: 18 msec ;; SERVER: ;; WHEN: Thu Aug 02 01:14:25 UTC 2018 ;; MSG SIZE rcvd: 44 – KennyM Aug 2 '18 at 1:24
  • Well.... I had a conditional right there: As long as the reverse zone is set up correctly. If the reverse zone is not set up correctly (or at all), then it's not going to work. – eftshift0 Aug 2 '18 at 1:34
  • @KennyM I cannot read the output from dig in your comment. Can you please edit your question and add it there formated? – Ingo Aug 2 '18 at 7:09
  • @Ingo the important part of the dig output is where it says QUERY: 1, ANSWER:0. – eftshift0 Aug 2 '18 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.