2

I want to get the fqdn from the hostname. I am am executiong this command on a linux server: python -c "import sys, socket; sys.stdout.write(socket.gethostbyaddr('')[0])" THis command works well and returns the fqdn from the hostname. But for some other servers, it returns this error: Traceback (most recent call last): File "", line 1, in socket.herror: [Errno 1] Unknown host

When i do "host " linux command, I get the fqdn of the failed host names with python command.

Anyone have a solution for that plz?

user3652134
  • 31
  • 1
  • 4
  • I got 3 IP on same interface and got 5 service. You want get host name but which interface(or clone interface) is current ? Define an ip address before getting hostname. Use `netifaces` module... – dsgdfg Apr 15 '16 at 11:56

1 Answers1

1

Difficulty using Python's socket.gethostbyaddr() covers this nicely.

The gist is that you must verify there's a PTR record. If not, you'll have to handle it via a try, except clause.

Specifically look at the part comparing the a valid and non-valid PTR records.

Community
  • 1
  • 1
nir0s
  • 1,151
  • 7
  • 17