Agent: Deduplicate timeout calculation in PingScanner

This commit is contained in:
Mike Salvatore 2021-08-19 13:25:52 -04:00
parent 198fbd66f8
commit 1d9372690d
1 changed files with 9 additions and 12 deletions

View File

@ -21,28 +21,22 @@ class PingScanner(HostScanner, HostFinger):
_SCANNED_SERVICE = "" _SCANNED_SERVICE = ""
def __init__(self): def __init__(self):
self._config = infection_monkey.config.WormConfiguration self._timeout = infection_monkey.config.WormConfiguration.ping_scan_timeout
if not "win32" == sys.platform:
self._timeout /= 1000
self._devnull = open(os.devnull, "w") self._devnull = open(os.devnull, "w")
self._ttl_regex = re.compile(TTL_REGEX_STR, re.IGNORECASE) self._ttl_regex = re.compile(TTL_REGEX_STR, re.IGNORECASE)
def is_host_alive(self, host): def is_host_alive(self, host):
timeout = self._config.ping_scan_timeout
if not "win32" == sys.platform:
timeout /= 1000
return 0 == subprocess.call( return 0 == subprocess.call(
["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(timeout), host.ip_addr], ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(self._timeout), host.ip_addr],
stdout=self._devnull, stdout=self._devnull,
stderr=self._devnull, stderr=self._devnull,
) )
def get_host_fingerprint(self, host): def get_host_fingerprint(self, host):
timeout = self._config.ping_scan_timeout ping_cmd = ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(self._timeout), host.ip_addr]
if not "win32" == sys.platform:
timeout /= 1000
ping_cmd = ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(timeout), host.ip_addr]
LOG.debug(f"Running ping command: {' '.join(ping_cmd)}") 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 # If stdout is not connected to a terminal (i.e. redirected to a pipe or file), the result
@ -77,3 +71,6 @@ class PingScanner(HostScanner, HostFinger):
LOG.debug("Error parsing ping fingerprint: %s", exc) LOG.debug("Error parsing ping fingerprint: %s", exc)
return False return False
def _build_ping_command(self, host):
return ["ping", PING_COUNT_FLAG, "1", PING_TIMEOUT_FLAG, str(self._timeout), host.ip_addr]