From 15e1470f6366e7e31522a7882781184817e55bcc Mon Sep 17 00:00:00 2001 From: Juan Vidal Date: Wed, 22 Feb 2017 10:09:28 +0000 Subject: Improve readability of ping function The function now uses return codes instead of parsing the output of the command. Unused options have been removed as well. Added a default retry_timeout for the command to avoid ping hangs. Change-Id: I8c1f0f03d8b1e3092743c8745399a08fa7df8e40 Signed-off-by: Juan Vidal --- sfc/lib/utils.py | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/sfc/lib/utils.py b/sfc/lib/utils.py index a7edef68..249cd6c8 100644 --- a/sfc/lib/utils.py +++ b/sfc/lib/utils.py @@ -177,31 +177,14 @@ def create_instance(nova_client, name, flavor, image_id, network_id, sg_id, return instance -def ping(remote, pkt_cnt=1, iface=None, retries=100, timeout=None): - ping_cmd = 'ping' - - if timeout: - ping_cmd = ping_cmd + ' -w %s' % timeout - - grep_cmd = "grep -e 'packet loss' -e rtt" - - if iface is not None: - ping_cmd = ping_cmd + ' -I %s' % iface - - ping_cmd = ping_cmd + ' -i 0 -c %d %s' % (pkt_cnt, remote) - cmd = ping_cmd + '|' + grep_cmd +def ping(remote, retries=100, retry_timeout=1): + cmd = 'ping -c1 -w{timeout} {remote}'.format( + timeout=retry_timeout, + remote=remote) while retries > 0: - _, output, _ = run_cmd(cmd) - if not output: - return False - - match = re.search('(\d*)% packet loss', output) - if not match: - return False - - packet_loss = int(match.group(1)) - if packet_loss == 0: + rc, _, _ = run_cmd(cmd) + if rc == 0: return True retries -= 1 -- cgit 1.2.3-korg