On 11/20/2013 02:19 PM, Viktor Mihajlovski wrote:
On 11/19/2013 02:25 PM, John Ferlan wrote:
> Using just socket.gethostbyaddr(socket.gethostname())[0] caused some
> issues recently in one of my DHCP testing environments. Breaking down
> the calls showed the following:
>
>>>> socket.gethostname()
> 'dhcp-186-211.bos.redhat.com'
>>>> socket.gethostbyname("dhcp-186-211.bos.redhat.com")
> '10.16.186.211'
>>>> socket.gethostbyaddr(socket.gethostname())[0]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> socket.herror: [Errno 1] Unknown host
>
> While just a socket.gethostname() could have worked, using the
> socket.getfqdn() seemed to be safer just in case.
> ---
> lib/VirtLib/live.py | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/lib/VirtLib/live.py b/lib/VirtLib/live.py
> index c929e71..6a10474 100644
> --- a/lib/VirtLib/live.py
> +++ b/lib/VirtLib/live.py
> @@ -100,6 +100,9 @@ def hostname(server):
> return out
>
> def full_hostname(server):
> - """To return the fully qualifiec domain name(FQDN) of the
system"""
> + """To return the fully qualified domain name(FQDN) of the
system"""
>
> - return socket.gethostbyaddr(socket.gethostname())[0]
> + if socket.getfqdn().find('.') >= 0:
> + return socket.getfqdn()
> + else:
> + return socket.gethostbyaddr(socket.gethostname())[0]
>
The server parameter seems to be entirely superfluous,
which makes me believe that the original intention might have
been something like (roughly)
return socket.gethostbyaddr(server)
which by itself will probably not solve the reverse lookup
issue you seem to have experienced
I think it was more of a cut-n-copy from hostname(server) just above it...
I did some digging, the function does show up in the initial git entry
for all of libvirt-cim, but none of the callers were using it. They
were using live.hostname() passing options.ip. Then commit id '7e998246'
changed the 3 callers to use full_hostname().
So how about this instead:
if socket.getfqdn().find('.') >= 0:
return socket.getfqdn()
elif socket.gethostname().find('.') >= 0:
return socket.gethostname()
else:
return socket.gethostbyaddr(server)[1][0]
I get the following:
server=localhost
socket.getfqdn()=dhcp-186-211.bos.redhat.com
socket.gethostname()=dhcp-186-211.bos.redhat.com
socket.gethostbyaddr(server)[1][0]=localhost.localdomain
From a python prompt:
>> import socket
>> socket.gethostbyaddr("localhost")
('localhost',
['localhost.localdomain', 'localhost6',
'localhost6.localdomain6'], ['::1'])
>>
Hence the [1][0] to get a FQDN rather than short name or the unexpected
tuple passed back to the caller...
All 3 tests will pass with the change in my configuration. I think one
of the keys is whether the name is found in /etc/hosts or not. Since I
use DHCP that won't happen.
John