ValentinBELYN / icmplib

Easily forge ICMP packets and make your own ping and traceroute.
GNU Lesser General Public License v3.0
266 stars 46 forks source link

Reverse Lookup? #49

Closed cldeluna closed 2 years ago

cldeluna commented 2 years ago

Hi, First, thank you. I am enjoying this module immensely! I love that you provide the privilege option. for some of us who have to work in locked down environments, this is key.

I have just updated my ping check script to use icmplib!

You recommend not using FQDNs. May I as why? Most of my inventory files use FQDNs which is why I ask.

This is not an issue but more of a feature request.

Would it be possible to add reverse lookup (get FQDN from IP address)?

ValentinBELYN commented 2 years ago

Hi @cldeluna 👋, I'm glad you like this library!

You recommend not using FQDNs. May I as why? Most of my inventory files use FQDNs which is why I ask.

FQDNs are not discouraged except for the multiping function (see https://github.com/ValentinBELYN/icmplib/issues/44). In short, icmplib provides several out-of-the-box functions including the multiping function. This function can resolve FQDNs or hostnames like the other ones. However, if one of them is not resolvable, the whole function stops (and raises an NameLookupError), which can be problematic depending on the context.

Example:

from icmplib import multiping, NameLookupError

domains = [
    'google.com',
    'dns.google.com',
    'this-domain-does-not-exist.tld',
    'github.com',
    'one.one.one.one'
]

try:
    hosts = multiping(domains, privileged=False)

    for host in hosts:
        if host.is_alive:
            print(f'{host.address} is up!')
        else:
            print(f'{host.address} is down!')
except NameLookupError as err:
    print(err)

Will output:

The name 'this-domain-does-not-exist.tld' cannot be resolved

To control exceptions more precisely, the same library provides a resolve function that you can use to resolve FQDNs and hostnames to IPs, which you can then pass to the multiping function.

Example:

from icmplib import multiping, resolve, NameLookupError

domains = [
    'google.com',
    'dns.google.com',
    'this-domain-does-not-exist.tld',
    'github.com',
    'one.one.one.one'
]

addresses = []

for domain in domains:
    try:
        address = resolve(domain)[0]
        addresses.append(address)
    except NameLookupError:
        print(f'{domain} does not exist!')

hosts = multiping(addresses, privileged=False)

for host in hosts:
    if host.is_alive:
        print(f'{host.address} is up!')
    else:
        print(f'{host.address} is down!')

Will output:

this-domain-does-not-exist.tld does not exist!
216.58.213.174 is up!
8.8.8.8 is up!
140.82.121.3 is up!
1.1.1.1 is up!

Would it be possible to add reverse lookup (get FQDN from IP address)?

icmplib focuses on the ICMP protocol and not on the DNS protocol 😉 Python already includes great tools to do this like socket.getnameinfo.

Example:

>>> import socket
>>> socket.getnameinfo(('8.8.8.8', 0), 0)[0]
'dns.google'
ValentinBELYN commented 2 years ago

Hi @cldeluna!

Thank you for your huge donation! ❤️ It's really gratifying to see that this project is appreciated. Sorry for the delay, I'm a little overwhelmed right now. I only saw your donation recently.

Would you like me to put your name on the README to thank you?