Chris Masone | 6a0680f | 2012-03-02 08:40:00 -0800 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | from autotest_lib.client.common_lib import base_utils |
| 6 | |
| 7 | def ping(host, deadline=None, tries=None, timeout=60): |
| 8 | """Attempt to ping |host|. |
| 9 | |
| 10 | Shell out to 'ping' to try to reach |host| for |timeout| seconds. |
| 11 | Returns exit code of ping. |
| 12 | |
| 13 | Per 'man ping', if you specify BOTH |deadline| and |tries|, ping only |
| 14 | returns 0 if we get responses to |tries| pings within |deadline| seconds. |
| 15 | |
| 16 | Specifying |deadline| or |count| alone should return 0 as long as |
| 17 | some packets receive responses. |
| 18 | |
| 19 | @param deadline: seconds within which |tries| pings must succeed. |
| 20 | @param tries: number of pings to send. |
| 21 | @param timeout: number of seconds after which to kill 'ping' command. |
| 22 | @return exit code of ping command. |
| 23 | """ |
| 24 | args = [host] |
| 25 | if deadline: |
| 26 | args.append('-w%d' % deadline) |
| 27 | if tries: |
| 28 | args.append('-c%d' % tries) |
| 29 | return base_utils.run('ping', args=args, |
| 30 | ignore_status=True, timeout=timeout, |
| 31 | stdout_tee=utils.TEE_TO_LOGS, |
| 32 | stderr_tee=utils.TEE_TO_LOGS).exit_status |