site_utils.py revision ae84354d058d9713d28bcc541cdce7e415e680af
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
5from autotest_lib.client.common_lib import base_utils
6
7def 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=base_utils.TEE_TO_LOGS,
32                          stderr_tee=base_utils.TEE_TO_LOGS).exit_status
33