[Libvirt-cim] [PATCH 01/10] live.full_hostname: Adjust mechanism to get FQDN

Xu Wang gesaint at linux.vnet.ibm.com
Fri May 9 07:17:26 UTC 2014


于 2014年05月08日 19:37, John Ferlan 写道:
> On 05/08/2014 05:01 AM, Xu Wang wrote:
>> 于 2014年04月14日 23:05, John Ferlan 写道:
>>> On 04/14/2014 02:15 AM, Xu Wang wrote:
>>>> 于 2014年04月05日 00:12, John Ferlan 写道:
>>>>> Rather than default to socket.gethostbyaddr(socket.gethostname())[0] to
>>>>> get full_hostname(), go through a sequence of steps to get a more
>>>>> correct result
>>>>>
>>>>> NOTE:
>>>>> Seehttp://www.redhat.com/archives/libvirt-cim/2013-November/msg00082.html
>>>>> for more details and history.
>>>>>
>>>>> Signed-off-by: John Ferlan<jferlan at redhat.com>
>>>>> ---
>>>>>     lib/VirtLib/live.py | 11 ++++++++---
>>>>>     1 file changed, 8 insertions(+), 3 deletions(-)
>>>>>
>>>>> diff --git a/lib/VirtLib/live.py b/lib/VirtLib/live.py
>>>>> index c929e71..e9cafc1 100644
>>>>> --- a/lib/VirtLib/live.py
>>>>> +++ b/lib/VirtLib/live.py
>>>>> @@ -100,6 +100,11 @@ def hostname(server):
>>>>>         return out
>>>>>     
>>>>>     def full_hostname(server):
>>>>> -    """To return the fully qualifiec domain name(FQDN) of the system"""
>>>>> -
>>>>> -    return socket.gethostbyaddr(socket.gethostname())[0]
>>>>> +    """To return the fully qualified domain name(FQDN) of the system"""
>>>>> +
>>>>> +    if socket.getfqdn().find('.') >= 0:
>>>>> +        return socket.getfqdn()
>>>>> +    elif socket.gethostname().find('.') >= 0:
>>>>> +        return socket.gethostname()
>>>>> +    else:
>>>>> +        return socket.gethostbyaddr(server)[1][0]
>>>> I got an error here. The content of my /etc/hosts is,
>>>>
>>>> # cat /etc/hosts
>>>> 127.0.0.1 RH64wenchao localhost localhost.localdomain localhost4
>>>> localhost4.localdomain4 #RH64wenchao
>>>>
>>> I don't see the same results if I add a different name to /etc/hosts. I
>>> didn't restart my network and that may make a difference.  I did restart
>>> my tog-pegasus, but that shouldn't make a difference, but who knows at
>>> this point.
>>>
>>> There's 3 places that use the returned data.  I'll play with this some
>>> more and see what happens
>>>
>>> John
>> Dear John,
>>     I installed several absolutely new systems to test it, and got
>> different results.
>> I have a question here. Why don't we use socket.gethostname() directly?
>> It seems
>> work well on my systems. Is there any other situation it could not handle?
>>
>> Thanks,
>>     Xu Wang
> The code currently does the following
>
> def full_hostname(server):
>      """To return the fully qualifiec domain name(FQDN) of the system"""
>
>      return socket.gethostbyaddr(socket.gethostname())[0]
>
>
> $ python
>>>> import socket
>>>> help(socket.getfqdn)
> Help on function getfqdn in module socket:
>
> getfqdn(name='')
>      Get fully qualified domain name from name.
>      
>      An empty argument is interpreted as meaning the local host.
>      
>      First the hostname returned by gethostbyaddr() is checked, then
>      possibly existing aliases. In case no FQDN is available, hostname
>      from gethostname() is returned.
Dear John,
   This is the description I got from 
https://docs.python.org/2/library/socket.html

   socket.getfqdn([name])
   Return a fully qualified domain name for name. If name is omitted or 
empty,
   it is interpreted as the local host. To find the fully qualified 
name, the
   hostname returned by gethostbyaddr() is checked, followed by aliases for
   the host, if available. The first name which includes a period is 
selected.
   In case no fully qualified domain name is available, the hostname as 
returned
   by gethostname() is returned.
   New in version 2.0.

   and

   socket.gethostname()
   Return a string containing the hostname of the machine where the Python
   interpreter is currently executing.
     If you want to know the current machine’s IP address, you may want
   to use gethostbyname(gethostname()). This operation assumes that 
there is
   a valid address-to-host mapping for the host, and the assumption does 
not
   always hold.
   Note: gethostname() doesn’t always return the fully qualified domain 
name;
   use getfqdn() (see above).

   I think socket.gethostname() returns the hostname of the machine 
where the
Python interpreter is currently executing contains potential issue. That 
is if
cimtest runs on different system with libvirt-cim, hostname maybe different.
Another point, gethostname() doesn't always return the fully qualified 
domain
name (Note mentioned, maybe different from getfqdn(). libvirt-cim set 
hostname
by calling get_fqdn(), in set_host_system_properties()).

   Hence, I agree with you about the getfqdn() is a proper choice.
>>>> help(socket.gethostbyaddr)
> Help on built-in function gethostbyaddr in module _socket:
>
> gethostbyaddr(...)
>      gethostbyaddr(host) -> (name, aliaslist, addresslist)
>      
>      Return the true host name, a list of aliases, and a list of IP addresses,
>      for a host.  The host argument is a string giving a host name or IP number.
>
>
> Thus the byaddr probably does what you asked for before in returning
> the aliaslist (e.g. the [1] entry) and then being able to do some sort of
> "if myname in aliaslist:" test.
>
> I still content getfqdn() is the proper action for the full_hostname()
> method.  A new method could be added "aliaslist" which could return all
> names in order to be checked.  It's just a matter of taking the time to
> do the code, check all the callers, etc.  Time that I haven't had lately.
>
> FWIW:
> My host (f20) returns:
>
>>>> print socket.getfqdn()
> localhost.localdomain
>>>> print socket.gethostname()
> localhost.localdomain
>>>> print socket.gethostbyaddr(socket.gethostname())
> ('localhost', ['localhost.localdomain', 'localhost6', 'localhost6.localdomain6'], ['::1'])
I got an interesting result if I changed the hostname when system 
starting the first time.

>>> import socket
>>> print socket.getfqdn()
zBX.testing
>>> print socket.gethostname()
zBX.testing
>>> print  socket.gethostbyaddr(socket.gethostname())
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
socket.gaierror: [Errno -2] Name or service not known

But my /etc/hosts was not changed (just as default),

# cat /etc/hosts
   127.0.0.1   localhost localhost.localdomain localhost4 
localhost4.localdomain4
   ::1         localhost localhost.localdomain localhost6 
localhost6.localdomain6

   Just the system with default hostname was changed from 
localhost.localdomain into
others could happened that. If we use getfqdn() that situation would not 
occur. But
I still don't know the reason why the error happened.

Thanks,
   Xu Wang
>>>> print socket.getfqdn()
> localhost.localdomain
>
>
> I've done no manipulation of my /etc/hosts
>
> John
>
> _______________________________________________
> Libvirt-cim mailing list
> Libvirt-cim at redhat.com
> https://www.redhat.com/mailman/listinfo/libvirt-cim

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/libvirt-cim/attachments/20140509/e8ce6aa7/attachment.htm>


More information about the Libvirt-cim mailing list