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