Exemple #1
0
    def ping(self):
        """Send an ICMP echo request and return True if success."""
        with subprocess.Popen(self._ping_cmd,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.DEVNULL) as pinger:
            try:
                pinger.communicate(timeout=1 + PING_TIMEOUT)
                return pinger.returncode == 0
            except subprocess.TimeoutExpired:
                kill_subprocess(pinger)
                return False

            except subprocess.CalledProcessError:
                return False
Exemple #2
0
async def test_kill_process():
    """Test killing a process."""
    sleeper = subprocess.Popen(
        "sleep 1000",
        shell=True,  # nosec # shell by design
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
    )
    pid = sleeper.pid

    assert os.kill(pid, 0) is None

    process.kill_subprocess(sleeper)

    with pytest.raises(OSError):
        os.kill(pid, 0)
Exemple #3
0
 def send_message(self, message="", **kwargs):
     """Send a message to a command line."""
     with subprocess.Popen(
             self.command,
             universal_newlines=True,
             stdin=subprocess.PIPE,
             shell=True,  # nosec # shell by design
     ) as proc:
         try:
             proc.communicate(input=message, timeout=self._timeout)
             if proc.returncode != 0:
                 _LOGGER.error("Command failed: %s", self.command)
         except subprocess.TimeoutExpired:
             _LOGGER.error("Timeout for command: %s", self.command)
             kill_subprocess(proc)
         except subprocess.SubprocessError:
             _LOGGER.error("Error trying to exec command: %s", self.command)
Exemple #4
0
 def ping(self):
     """Send ICMP echo request and return details if success."""
     pinger = subprocess.Popen(self._ping_cmd,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
     try:
         out = pinger.communicate(timeout=self._count + PING_TIMEOUT)
         _LOGGER.debug("Output is %s", str(out))
         if sys.platform == "win32":
             match = WIN32_PING_MATCHER.search(str(out).split("\n")[-1])
             rtt_min, rtt_avg, rtt_max = match.groups()
             return {
                 "min": rtt_min,
                 "avg": rtt_avg,
                 "max": rtt_max,
                 "mdev": ""
             }
         if "max/" not in str(out):
             match = PING_MATCHER_BUSYBOX.search(str(out).split("\n")[-1])
             rtt_min, rtt_avg, rtt_max = match.groups()
             return {
                 "min": rtt_min,
                 "avg": rtt_avg,
                 "max": rtt_max,
                 "mdev": ""
             }
         match = PING_MATCHER.search(str(out).split("\n")[-1])
         rtt_min, rtt_avg, rtt_max, rtt_mdev = match.groups()
         return {
             "min": rtt_min,
             "avg": rtt_avg,
             "max": rtt_max,
             "mdev": rtt_mdev
         }
     except subprocess.TimeoutExpired:
         kill_subprocess(pinger)
         return False
     except (subprocess.CalledProcessError, AttributeError):
         return False