summaryrefslogtreecommitdiff
path: root/bin
diff options
authorDaniel Manrique <roadmr@ubuntu.com>2014-06-10 13:25:08 -0400
committerDaniel Manrique <roadmr@ubuntu.com>2014-06-10 13:25:08 -0400
commitb5162c529586e007ceceb90bbd70a6ad1715da18 (patch)
treea79d53f0125ca061954cb0d0bbe09f6bfe2d3f66 /bin
parentc50d971c1e6b7ff804f1b8e7d6f7a371893578aa (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).
Diffstat (limited to 'bin')
-rwxr-xr-xbin/gateway_ping_test32
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