1

I have create a script to resolve IP to hostname. The script does not resolve the hostname, it gives the following error:

cannot resolve hostname: 10.10.10.10 [Errno 11004] getaddrinfo failed cannot resolve hostname: 10.10.10.10 [Errno 11004] getaddrinfo failed

Please suggest. I'm new to python. The text file contains more than 1000 IPs.

#!/usr/bin/python
import socket
pfile = open ('C:\\Python27\\scripts\\test.txt')
while True:
    IP = pfile.readline()
    if not IP:
        break
    try:
        host = socket.gethostbyaddr("IP")
        print host, IP
    except socket.gaierror, err:
        print "cannot resolve hostname: ", IP, err
pfile.close()
user2820987
  • 55
  • 1
  • 2
  • 7
  • 1
    Does it work for any of them? Did you try something simpler, without the file? – Ry- Sep 26 '13 at 19:53
  • 6
    `"IP"` is a string, you probably wanted to pass `IP` at the least. – FatalError Sep 26 '13 at 19:54
  • http://stackoverflow.com/questions/7334199/getaddrinfo-failed-what-does-that-mean – Alex W Sep 26 '13 at 19:55
  • There are some errors in this code. For example `socket.gethostbyaddr("IP")` will never work, because you check for the host of the string "IP" instead of the variable IP. – Thomas Skowron Sep 26 '13 at 19:55
  • Tried with IP does not work same type of error message. – user2820987 Sep 26 '13 at 19:57
  • You likely need to `.strip()` the IP variable. If your file is line delimited, you likely have New Line/Return chars in the IP var. As well as replace `"IP"` with `IP` as others have suggested. – tMC Sep 26 '13 at 20:11

4 Answers4

4

There are two problems here.

First, as FatalError pointed out, you're not looking up the value of the IP variable, but the string "IP".

Second, pfile.readline() is going to leave a trailing newline at the end of the IP string, so it's still going to fail.

So:

host = socket.gethostbyaddr(IP.rstrip())

Also, on some platforms, if your DNS isn't working, gethostbyaddr will fail even when given an IP address. So, you may want to do a simple test on the machine you're running the script on (if it's not the same machine you're already using for SO)—e.g., open a browser and go to Google.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • It works but here is the output, there are some errors ('testhostname', [], ['10.10.10.10']) 10.10.10.10 Traceback (most recent call last): File "C:\Python27\scripts\hosttoip.py", line 11, in host = socket.gethostbyaddr(IP.rstrip()) socket.herror: [Errno 11004] host not found – user2820987 Sep 26 '13 at 20:02
  • @user2820987: There's no way the code you showed us could get to the `print host, IP` successfully but still raised an exception on the `gethostbyaddr` line. Maybe your real code has two `gethostbyaddr` calls, and the first one is working but the second is not? – abarnert Sep 26 '13 at 21:31
0

As far as I can tell, there are different problems.

The line:

host = socket.gethostbyaddr("IP")

will fail because of the string. To fix this, use host = socket.gethostbyaddr(IP).

Furthermore, the error you posted here is caused by 10.10.10.10 being a private IP. The ranges 10.0.0.0–10.255.255.255, 172.16.0.0–172.31.255.255 and 192.168.255.255 are private network blocks; socket.gethostbyaddr() is not able to resolve these addresses. See https://www.rfc-editor.org/rfc/rfc1918 for more information about private blocks.

Community
  • 1
  • 1
Boisei0
  • 24
  • 1
  • 1
  • 4
  • 1
    `gethostbyaddr()`, etc. don't really care about whether it's a private-block IP address or not. If there's an entry in DNS, your host file, LDAP, etc., for 10.10.10.10, it should resolve. Actual network packets containing that IP address as source or destination, however, should never be allowed to escape your local network onto the Internet. – twalberg Sep 26 '13 at 20:17
  • @twalberg: And in fact, even if there's no entry, it should still resolve to itself. (On some platforms, if there is a DNS server but it's not responding, you may still get an error, but not otherwise.) But most organizations that use 10.x.x.x addresses for inside the NAT/firewall/etc. also have an internal DNS server for those ranges, and that's what will respond to the request, so it won't be a problem. – abarnert Sep 26 '13 at 21:30
0

After a little Googling, I got it to work in Python 3 as follows:

import socket
pfile = open ('C:\\TEMP\\IPs.txt')
while True:
  IP = pfile.readline()
  try:
    host = socket.gethostbyaddr(IP.rstrip())
    print(IP,host)
  except Exception as e:
    print(IP,"NULL")
pfile.close()
Gavin
  • 1
-2

This works:

import socket


IP = "www.google.ca"
host = socket.gethostbyaddr(IP)
print host, IP
jramirez
  • 8,537
  • 7
  • 33
  • 46