Btw from documentation:
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 also found that when you mess with /etc/hosts, gethostname() might
return that result instead. Can you review it's content? Anyway to me it
looks like a clear bug. It's advised in documentation and widely used.
Regards,
Lukáš
Dne 2.4.2015 v 09:13 Lukáš Doktor napsal(a):
Hello Lucas,
I (quickly) searched for the bug in bugzilla and found only info about
qpid-tools moving from gethostbyname() to getaddrinfo():
https://bugzilla.redhat.com/show_bug.cgi?id=489470
Google search showed this:
import socket
[i[4][0] for i in socket.getaddrinfo(socket.gethostname(), None)]
then it's just a matter of good condition to find useful address and
return it. It works fine on F20, can you give it a try on F22? Although
can you please send us the link to bugzilla where you reported this
issue? I'm avid for details...
Regards,
Lukáš
PS: I'd rather use bash tool to obtain IP address then relying on
external, publicly available address.
Dne 2.4.2015 v 03:05 Lucas Meneghel Rodrigues napsal(a):
Hello good friends,
I've installed Fedora 22 on a new work laptop today and started to do my
usual setup to get things going.
I've realized that virt-test is not working on Fedora, because the good
ol' trick of using
socket.gethostbyname(socket.gethostname())
Is not working for F22, as it returns some bogus value, such as
92.242.140.67.
I've been thinking of solutions, but the only one I could find that
would possibly work in other hosts does require a connection to a
network resource of some sort, such as 'redhat.com' (see below).
Now, this is not quite ideal, so I'm writing this e-mail to see if any
of you has a good idea that we could use.
Or maybe this is some bug in Fedora 22 that is yet to be fixed. Anyway,
some food for thought.
Cheers,
Lucas
From 0e04529d57ece9dedfe9e428deb95eb6d6b687da Mon Sep 17 00:00:00 2001
From: Lucas Meneghel Rodrigues <[email protected]>
Date: Wed, 1 Apr 2015 21:35:58 -0300
Subject: [PATCH] virttest.virt_vm: Alternate method for locating local
machine
IP
Sometimes, socket.gethostbyname(socket.gethostname()) just doesn't
cut it, as I found it to be the case in my fresh Fedora 22 install.
One proposal of fixing this would be this fairly unorthodox procedure,
which is to establish a socket, connect to an internet address and
check the socket address afterwards. This only works if we have
an internet connection, though.
n
Signed-off-by: Lucas Meneghel Rodrigues <[email protected]>
---
virttest/virt_vm.py | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/virttest/virt_vm.py b/virttest/virt_vm.py
index 5ebf6be..1933940 100644
--- a/virttest/virt_vm.py
+++ b/virttest/virt_vm.py
@@ -642,8 +642,9 @@ class BaseVM(object):
self.ip_version = self.params.get("ip_version", "ipv4").lower()
# TODO: Determine port redirection in use w/o checking nettype
if nic.nettype not in ['bridge', 'macvtap']:
- hostname = socket.gethostname()
- return socket.gethostbyname(hostname)
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ s.connect(('redhat.com', 0))
+ return socket.gethostbyname(s.getsockname()[0])
if not nic.has_key('mac') and self.params.get('vm_type') ==
'libvirt':
# Look it up from xml
nic.mac = self.get_virsh_mac_address(index)
--
2.3.4
_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel