diff mbox

[1/4] client.bin.net.net_utils: Introduce get_local_ip()

Message ID 1306220891-3993-2-git-send-email-lmr@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Lucas Meneghel Rodrigues May 24, 2011, 7:08 a.m. UTC
Get ip address in local system which can communicate
with a given ip, that will be useful for subtests like
netperf2.

Signed-off-by: Jiri Zupka <jzupka@redhat.com>
---
 client/bin/net/net_utils.py |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)
diff mbox

Patch

diff --git a/client/bin/net/net_utils.py b/client/bin/net/net_utils.py
index 868958c..7c96ba0 100644
--- a/client/bin/net/net_utils.py
+++ b/client/bin/net/net_utils.py
@@ -5,6 +5,7 @@  This library is to release in the public repository.
 
 import commands, os, re, socket, sys, time, struct
 from autotest_lib.client.common_lib import error
+from autotest_lib.client.bin import utils as client_utils
 import utils
 
 TIMEOUT = 10 # Used for socket timeout and barrier timeout
@@ -27,6 +28,22 @@  class network_utils(object):
         utils.system('/sbin/ifconfig -a')
 
 
+    def get_ip_local(self, query_ip, netmask="24"):
+        """
+        Get ip address in local system which can communicate with query_ip.
+
+        @param query_ip: IP of client which wants to communicate with
+                autotest machine.
+        @return: IP address which can communicate with query_ip
+        """
+        ip = client_utils.system_output("ip addr show to %s/%s" %
+                                        (query_ip, netmask))
+        ip = re.search(r"inet ([0-9.]*)/",ip)
+        if ip is None:
+            return ip
+        return ip.group(1)
+
+
     def disable_ip_local_loopback(self, ignore_status=False):
         utils.system("echo '1' > /proc/sys/net/ipv4/route/no_local_loopback",
                      ignore_status=ignore_status)