159
159
def ping(host, interface, count, deadline, verbose=False):
160
command = "ping -c %s -w %s %s" % (count, deadline, host)
161
command = ["ping", str(host), "-c", str(count), "-w", str(deadline)]
162
command = ("ping -I%s -c %s -w %s %s"
163
% (interface, count, deadline, host))
163
command.append("-I{}".format(interface))
165
165
reg = re.compile(r"(\d+) packets transmitted, (\d+) received, (\d+)% packet loss")
166
ping_summary = {'transmitted': 0, 'received': 0, 'pct_loss': 0}
168
output = os.popen(command)
169
for line in output.readlines():
169
output = subprocess.check_output(command, universal_newlines=True)
170
except FileNotFoundError as excp:
171
# No ping command present; default exception message is
172
# informative enough.
174
except subprocess.CalledProcessError as excp:
175
# Ping returned fail exit code
176
print("ERROR: ping result: {}".format(excp))
173
received = re.findall(reg, line)
181
received = re.findall(reg, output)
175
183
ping_summary = received[0]
177
ping_summary={'transmitted': int(ping_summary[0]),
178
'received': int(ping_summary[1]),
179
'pct_loss': int(ping_summary[2])}
185
ping_summary={'transmitted': int(ping_summary[0]),
186
'received': int(ping_summary[1]),
187
'pct_loss': int(ping_summary[2])}
180
188
return ping_summary