blob: ec84577df0556783c1ff35aea877db0cd0de3afe [file] [log] [blame]
Chris Masone6a0680f2012-03-02 08:40:00 -08001# 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.
Fang Deng7c2be102012-08-27 16:20:25 -07004import re
Scott Zawalski347a0b82012-03-30 16:39:21 -04005import socket
Chris Masone6a0680f2012-03-02 08:40:00 -08006
Scott Zawalski347a0b82012-03-30 16:39:21 -04007from autotest_lib.client.common_lib import base_utils, global_config
Chris Masone6a0680f2012-03-02 08:40:00 -08008
9def ping(host, deadline=None, tries=None, timeout=60):
10 """Attempt to ping |host|.
11
12 Shell out to 'ping' to try to reach |host| for |timeout| seconds.
13 Returns exit code of ping.
14
15 Per 'man ping', if you specify BOTH |deadline| and |tries|, ping only
16 returns 0 if we get responses to |tries| pings within |deadline| seconds.
17
18 Specifying |deadline| or |count| alone should return 0 as long as
19 some packets receive responses.
20
21 @param deadline: seconds within which |tries| pings must succeed.
22 @param tries: number of pings to send.
23 @param timeout: number of seconds after which to kill 'ping' command.
24 @return exit code of ping command.
25 """
26 args = [host]
27 if deadline:
28 args.append('-w%d' % deadline)
29 if tries:
30 args.append('-c%d' % tries)
31 return base_utils.run('ping', args=args,
32 ignore_status=True, timeout=timeout,
Scott Zawalskiae843542012-03-20 09:51:29 -040033 stdout_tee=base_utils.TEE_TO_LOGS,
34 stderr_tee=base_utils.TEE_TO_LOGS).exit_status
Scott Zawalski347a0b82012-03-30 16:39:21 -040035
36
37def host_is_in_lab_zone(hostname):
38 """Check if the host is in the CROS.dns_zone.
39
40 @param hostname: The hostname to check.
41 @returns True if hostname.dns_zone resolves, otherwise False.
42 """
43 host_parts = hostname.split('.')
44 dns_zone = global_config.global_config.get_config_value('CROS', 'dns_zone',
45 default=None)
46 fqdn = '%s.%s' % (host_parts[0], dns_zone)
47 try:
48 socket.gethostbyname(fqdn)
49 return True
50 except socket.gaierror:
51 return False
Fang Deng7c2be102012-08-27 16:20:25 -070052
53
54def get_current_board():
55 """Return the current board name.
56
57 @return current board name, e.g "lumpy", None on fail.
58 """
59 with open('/etc/lsb-release') as lsb_release_file:
60 for line in lsb_release_file:
61 m = re.match(r'^CHROMEOS_RELEASE_BOARD=(.+)$', line)
62 if m:
63 return m.group(1)
64 return None