Agent: Generate PingScanEvent timestamp closer to ping command

This commit is contained in:
Mike Salvatore 2022-09-28 16:02:18 -04:00 committed by Shreya Malviya
parent 0cd8cd577d
commit 2eee427901
1 changed files with 7 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import subprocess
import sys import sys
from ipaddress import IPv4Address from ipaddress import IPv4Address
from time import time from time import time
from typing import Tuple
from common import OperatingSystem from common import OperatingSystem
from common.agent_events import PingScanEvent from common.agent_events import PingScanEvent
@ -34,9 +35,7 @@ def _ping(host: str, timeout: float, agent_event_queue: IAgentEventQueue) -> Pin
if is_windows_os(): if is_windows_os():
timeout = math.floor(timeout * 1000) timeout = math.floor(timeout * 1000)
event_timestamp = time() event_timestamp, ping_command_output = _run_ping_command(host, timeout)
ping_command_output = _run_ping_command(host, timeout)
ping_scan_data = _process_ping_command_output(ping_command_output) ping_scan_data = _process_ping_command_output(ping_command_output)
logger.debug(f"{host} - {ping_scan_data}") logger.debug(f"{host} - {ping_scan_data}")
@ -47,7 +46,7 @@ def _ping(host: str, timeout: float, agent_event_queue: IAgentEventQueue) -> Pin
return ping_scan_data return ping_scan_data
def _run_ping_command(host: str, timeout: float) -> str: def _run_ping_command(host: str, timeout: float) -> Tuple[float, str]:
ping_cmd = _build_ping_command(host, timeout) ping_cmd = _build_ping_command(host, timeout)
logger.debug(f"Running ping command: {' '.join(ping_cmd)}") logger.debug(f"Running ping command: {' '.join(ping_cmd)}")
@ -55,6 +54,8 @@ def _run_ping_command(host: str, timeout: float) -> str:
# of os.device_encoding(1) will be None. Setting errors="backslashreplace" prevents a crash # of os.device_encoding(1) will be None. Setting errors="backslashreplace" prevents a crash
# in this case. See #1175 and #1403 for more information. # in this case. See #1175 and #1403 for more information.
encoding = os.device_encoding(1) encoding = os.device_encoding(1)
ping_event_timestamp = time()
sub_proc = subprocess.Popen( sub_proc = subprocess.Popen(
ping_cmd, ping_cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
@ -74,9 +75,9 @@ def _run_ping_command(host: str, timeout: float) -> str:
logger.debug(output) logger.debug(output)
except subprocess.TimeoutExpired as te: except subprocess.TimeoutExpired as te:
logger.error(te) logger.error(te)
return "" return ping_event_timestamp, ""
return output return ping_event_timestamp, output
def _process_ping_command_output(ping_command_output: str) -> PingScanData: def _process_ping_command_output(ping_command_output: str) -> PingScanData: