diff options
| author | Daniel Manrique <roadmr@ubuntu.com> | 2014-06-11 12:16:24 +0000 |
|---|---|---|
| committer | Daniel Manrique <> | 2014-06-11 12:16:24 +0000 |
| commit | 84d9bf5f2ca4320174b67fcfc32fdc2780c39aa2 (patch) | |
| tree | 6e8513bbf933bdb9699bc5bcd444d187780b01cf | |
| parent | 418c060c963595084654e50925b153c20714ce75 (diff) | |
| parent | b5162c529586e007ceceb90bbd70a6ad1715da18 (diff) | |
" providers:checkbox: Minor refactoring of gateway_ping_test.
Rewrote the way ping command is invoked and the output is processed to solve a few problems (LP: #1232774). [r=zkrynicki][bug=1232774][author=roadmr]"
| -rwxr-xr-x | bin/gateway_ping_test | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/bin/gateway_ping_test b/bin/gateway_ping_test index 19d9f2b..da7ed2d 100755 --- a/bin/gateway_ping_test +++ b/bin/gateway_ping_test @@ -157,26 +157,34 @@ def get_host_to_ping(interface=None, verbose=False, default=None): def ping(host, interface, count, deadline, verbose=False): - command = "ping -c %s -w %s %s" % (count, deadline, host) + + command = ["ping", str(host), "-c", str(count), "-w", str(deadline)] if interface: - command = ("ping -I%s -c %s -w %s %s" - % (interface, count, deadline, host)) + command.append("-I{}".format(interface)) reg = re.compile(r"(\d+) packets transmitted, (\d+) received, (\d+)% packet loss") - ping_summary = None - - output = os.popen(command) - for line in output.readlines(): + ping_summary = {'transmitted': 0, 'received': 0, 'pct_loss': 0} + + try: + output = subprocess.check_output(command, universal_newlines=True) + except FileNotFoundError as excp: + # No ping command present; default exception message is + # informative enough. + print(excp) + except subprocess.CalledProcessError as excp: + # Ping returned fail exit code + print("ERROR: ping result: {}".format(excp)) + else: if verbose: - print(line.rstrip()) + print(output) - received = re.findall(reg, line) + received = re.findall(reg, output) if received: ping_summary = received[0] - ping_summary={'transmitted': int(ping_summary[0]), - 'received': int(ping_summary[1]), - 'pct_loss': int(ping_summary[2])} + ping_summary={'transmitted': int(ping_summary[0]), + 'received': int(ping_summary[1]), + 'pct_loss': int(ping_summary[2])} return ping_summary |
