xref: /aosp_15_r20/external/toolchain-utils/cros_utils/machines.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1# -*- coding: utf-8 -*-
2# Copyright 2015 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Utilities relating to machine-specific functions."""
7
8
9from cros_utils import command_executer
10
11
12def MachineIsPingable(machine, logging_level="average"):
13    """Checks to see if a machine is responding to 'ping'.
14
15    Args:
16      machine: String containing the name or ip address of the machine to check.
17      logging_level: The logging level with which to initialize the
18        command_executer (from command_executor.LOG_LEVEL enum list).
19
20    Returns:
21      Boolean indicating whether machine is responding to ping or not.
22    """
23    ce = command_executer.GetCommandExecuter(log_level=logging_level)
24    cmd = "ping -c 1 -w 3 %s" % machine
25    status = ce.RunCommand(cmd)
26    return status == 0
27