2

I am currently trying to build a little Networkscan for my home network. To resolve the hostnames, I want to use this function called within a for loop iterating over a range of IPs. That is doing very well, but I think the gethostbyaddr(tgtHost) function is very very slow. Is there a way to speed things up or use alternative functions ?

def fn_hostscan(tgtHost):

    response = os.system("ping -n 1 " + tgtHost + "> C:\\temp\log.txt")

    if response == 0:
        try:
            tgtName = gethostbyaddr(tgtHost)
            print("\n[+] " + tgtHost + " = " + tgtName[0])
        except:
            dummy = 0
    else:

    print("\n[-]" + tgtHost + " = None found") 
alexwlchan
  • 5,699
  • 7
  • 38
  • 49
Lucas
  • 21
  • 3

2 Answers2

1

Taking a look at whats around for this:

Would this suit your purposes? - Finding local IP addresses using Python's stdlib

import socket 
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("gmail.com",80))
print(s.getsockname()[0])
s.close()
Community
  • 1
  • 1
Kelvin
  • 1,357
  • 2
  • 11
  • 22
1

gethostbyaddr and similar functions are dependant of your resolv.conf (see http://linux.die.net/man/5/resolv.conf).

  1. your DNS resolver is slow (try using Google's Public DNS)
  2. some IPs do not have PTR records or their NS are offline, the default timeout is 5sec - keep that in mind while "looping"

To improve performance you might want to parallelize it, use a fast resolver and keep the timeout setting to a minimum.

If you can, use libs similar to http://c-ares.haxx.se/

lifeofguenter
  • 1,121
  • 1
  • 13
  • 22