diff options
author | Juan Vidal <juan.vidal.allende@ericsson.com> | 2017-02-22 10:09:28 +0000 |
---|---|---|
committer | Juan Vidal <juan.vidal.allende@ericsson.com> | 2017-02-22 10:09:28 +0000 |
commit | 15e1470f6366e7e31522a7882781184817e55bcc (patch) | |
tree | 321819dff68f1023a142d32e6ec4a8eb8db9891b | |
parent | 371685974781964b0d5339f8497c06c89b24ae20 (diff) |
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 <juan.vidal.allende@ericsson.com>
-rw-r--r-- | sfc/lib/utils.py | 29 |
1 files 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 |