IPToHost

Try to get the hostname correlated with the input IP.

python · Common Scripts

Source

import socket

import demistomock as demisto  # noqa: F401
from CommonServerPython import *  # noqa: F401


def ip_to_host(ip: str) -> CommandResults:
    host_info = socket.gethostbyaddr(ip)

    if not host_info:
        raise DemistoException("No results were found for the given value.")

    hostname = host_info[0]

    if str(hostname) == ip:
        return CommandResults(readable_output=f"The IP address {ip} has no associated hostname.")

    output = {"Hostname": str(hostname), "IP": ip}

    md = tableToMarkdown("IP to Host", [output])

    return CommandResults(
        outputs=output,
        outputs_prefix="Endpoint",
        outputs_key_field="Hostname",
        readable_output=md,
    )


def main():
    try:
        ip = demisto.args().get("ip")
        return_results(ip_to_host(ip))
    except Exception as e:
        demisto.error(traceback.format_exc())
        return_error(f"Couldn't get the IP host info. Error information: {e!s}")


if __name__ in ("__main__", "__builtin__", "builtins"):
    main()

README

Gets the hostname correlated with the inputed IP address.

Script Data


Name Description
Script Type python
Tags -

Inputs


Argument Name Description
ip The IP address to check.

Outputs


Path Description Type
Endpoint The endpoint object. Unknown
Endpoint.Hostname The endpoint hostname. string
Endpoint.IP The endpoint IP address. string