diff --git a/test_testping1.py b/test_testping1.py index cc3f6a8..93bfd3b 100644 --- a/test_testping1.py +++ b/test_testping1.py @@ -95,7 +95,7 @@ def test_is_reachable_calls_ping_correctly(self, mock_call): is_reachable('192.168.1.1', timeout=5) # Verify that subprocess.call was called with the correct arguments, including the timeout mock_call.assert_called_once_with( - ['ping', '-c', '1', '-W', '5', '192.168.1.1'], + ['ping', '-n', '-c', '1', '-W', '5', '192.168.1.1'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=7 ) diff --git a/testping1.py b/testping1.py index 16af1e0..af17c90 100644 --- a/testping1.py +++ b/testping1.py @@ -33,7 +33,11 @@ def is_reachable(ip, timeout=1): logging.error(f"Invalid timeout value: {repr(timeout)}") return False - command = ["ping", "-c", "1", "-W", str(timeout_val), str(ip_obj)] # -W for timeout in seconds (Linux) + # ⚡ Bolt: Optimized ping execution by adding `-n` flag. + # The `-n` flag skips reverse DNS resolution. Without it, ping attempts to + # resolve the hostname for every IP, which can cause multi-second delays + # (even with a 1s timeout) if the IP lacks a PTR record or DNS is unresponsive. + command = ["ping", "-n", "-c", "1", "-W", str(timeout_val), str(ip_obj)] # -W for timeout in seconds (Linux) # ⚡ Bolt: Optimized ping execution by using subprocess.call and redirecting # output to DEVNULL instead of using Popen with PIPE.