From 1f519ad1ee0033578fd76362d7c0de90a3dfa8bf Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 19 Aug 2021 13:27:40 -0400 Subject: [PATCH] Agent: Deduplicate ping command list in PingScanner --- monkey/infection_monkey/network/ping_scanner.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/monkey/infection_monkey/network/ping_scanner.py b/monkey/infection_monkey/network/ping_scanner.py index ef1534b53..d7ebffb56 100644 --- a/monkey/infection_monkey/network/ping_scanner.py +++ b/monkey/infection_monkey/network/ping_scanner.py @@ -29,14 +29,17 @@ class PingScanner(HostScanner, HostFinger): self._ttl_regex = re.compile(TTL_REGEX_STR, re.IGNORECASE) def is_host_alive(self, host): + ping_cmd = self._build_ping_command(host.ip_addr) + LOG.debug(f"Running ping command: {' '.join(ping_cmd)}") + return 0 == subprocess.call( - ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(self._timeout), host.ip_addr], + ping_cmd, stdout=self._devnull, stderr=self._devnull, ) def get_host_fingerprint(self, host): - ping_cmd = ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(self._timeout), host.ip_addr] + ping_cmd = self._build_ping_command(host.ip_addr) LOG.debug(f"Running ping command: {' '.join(ping_cmd)}") # If stdout is not connected to a terminal (i.e. redirected to a pipe or file), the result @@ -72,5 +75,5 @@ class PingScanner(HostScanner, HostFinger): return False - def _build_ping_command(self, host): - return ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(self._timeout), host.ip_addr] + def _build_ping_command(self, ip_addr): + return ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(self._timeout), ip_addr]