Ping

Pings an IP or url address, to verify it's up. Note - On Cortex XSOAR 8 and Cortex XSIAM, the script can run only on a custom engine.

python · Common Scripts

Details

IDPing
Languagepython
From Version5.0.0
Docker Imagedemisto/netutils:1.0.0.11867191
TagsUtility

README

Pings an IP or url address, to verify it’s up. Note - On Cortex XSOAR 8 and Cortex XSIAM, the script can run only on a custom engine.

Script Data


Name Description
Script Type python3
Tags Utility
Cortex XSOAR Version 5.0.0

Inputs


Argument Name Description
address Address to ping

Outputs


Path Description Type
Ping.ret_code Ping return code number
Ping.destination Ping destination address string
Ping.max_rtt Ping max round trip time number
Ping.avg_rtt Ping average round trip time number
Ping.min_rtt Ping minimum round trip time number
Ping.destination_ip Ping destination IP string
import os
import re

import demistomock as demisto
import pytest
from CommonServerPython import entryTypes
from Ping import main

# To run the tests in an editor see: test_data/ping

RETURN_ERROR_TARGET = "Ping.return_error"


@pytest.mark.parametrize("address", ["google.com", "8.8.8.8"])
def test_ping(mocker, address):
    if os.getenv("GITHUB_ACTIONS"):
        pytest.skip("Ping cannot be executed from github actions because they block ICMP packets")
    mocker.patch.object(demisto, "args", return_value={"address": address})
    mocker.patch.object(demisto, "results")
    # validate our mocks are good
    assert demisto.args()["address"] == address
    main()
    assert demisto.results.call_count == 1
    # call_args is tuple (args list, kwargs). we only need the first one
    results = demisto.results.call_args[0]
    assert len(results) == 1
    assert results[0]["Type"] == entryTypes["note"]
    assert address in results[0]["EntryContext"]["Ping"]["destination"]
    assert re.fullmatch(r"\d+\.\d+\.\d+.\d+", results[0]["EntryContext"]["Ping"]["destination_ip"]) is not None
    assert results[0]["EntryContext"]["Ping"]["mdev_rtt"]


def test_ping_mocked(mocker):
    import subprocess

    address = "8.8.8.8"
    mocker.patch.object(demisto, "args", return_value={"address": address})
    mocker.patch.object(demisto, "results")
    mocker.patch.object(
        subprocess,
        "check_output",
        return_value=f"PING {address} ({address}) 56(84) bytes of data.\n--- {address} ping statistics ---\n3 "
        f"packets transmitted, 3 received, 0% packet loss, time 2010ms"
        f"\nrtt min/avg/max/mdev = 12.392/16.995/22.088/3.973 ms",
    )
    # validate our mocks are good
    assert demisto.args()["address"] == address
    main()
    assert demisto.results.call_count == 1
    # call_args is tuple (args list, kwargs). we only need the first one
    results = demisto.results.call_args[0]
    assert len(results) == 1
    assert results[0]["Type"] == entryTypes["note"]
    assert address in results[0]["EntryContext"]["Ping"]["destination"]
    assert re.fullmatch(r"\d+\.\d+\.\d+.\d+", results[0]["EntryContext"]["Ping"]["destination_ip"]) is not None
    assert results[0]["EntryContext"]["Ping"]["mdev_rtt"]


def test_fail_ping(mocker):
    mocker.patch.object(demisto, "args", return_value={"address": "nonExistingDomain45343.com"})  # disable-secrets-detection
    return_error_mock = mocker.patch(RETURN_ERROR_TARGET)
    main()
    assert return_error_mock.call_count == 1
    # call_args last call with a tuple of args list and kwargs
    err_msg = return_error_mock.call_args[0][0]
    assert "Name does not resolve" in err_msg or "Name or service not known" in err_msg


def test_fail_ping_permission_error_xsoar8(mocker):
    """
    Given: ping which cannot be executed on engine0

    When: running ping script

    Then: Ensure that error indicating that ping can only run on custom engines
    """
    import subprocess

    mocker.patch.object(subprocess, "check_output", side_effect=Exception("ping: socket: Operation not permitted"))
    mocker.patch.object(demisto, "args", return_value={"address": "8.8.8.8"})
    return_error_mock = mocker.patch(RETURN_ERROR_TARGET)
    mocker.patch("Ping.is_xsoar_on_prem", return_value=False)
    main()

    err_msg = return_error_mock.call_args[0][0]
    assert "The Ping script can be executed only on custom engines" in err_msg