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:
See http://www.redhat.com/archives/libvirt-cim/2013-November/msg00082.html
for more details and history.
Signed-off-by: John Ferlan <jferlan@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,
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.