1
votes

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?

1
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

1 Answers

1
votes

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.