privex.helpers.net

Network related helper code

Copyright:

    +===================================================+
    |                 © 2019 Privex Inc.                |
    |               https://www.privex.io               |
    +===================================================+
    |                                                   |
    |        Originally Developed by Privex Inc.        |
    |                                                   |
    |        Core Developer(s):                         |
    |                                                   |
    |          (+)  Chris (@someguy123) [Privex]        |
    |          (+)  Kale (@kryogenic) [Privex]          |
    |                                                   |
    +===================================================+

Copyright 2019     Privex Inc.   ( https://www.privex.io )

Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal in 
the Software without restriction, including without limitation the rights to use, 
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Functions

asn_to_name(as_number[, quiet])

Look up an integer Autonomous System Number and return the human readable name of the organization.

ip4_to_rdns(ip_obj[, v4_boundary, boundary])

Internal function for getting the rDNS domain for a given v4 address.

ip6_to_rdns(ip_obj[, v6_boundary, boundary])

Internal function for getting the rDNS domain for a given v6 address.

ip_is_v4(ip)

Determines whether an IP address is IPv4 or not

ip_is_v6(ip)

Determines whether an IP address is IPv6 or not

ip_to_rdns(ip[, boundary, v6_boundary, …])

Converts an IPv4 or IPv6 address into an in-addr domain

privex.helpers.net.asn_to_name(as_number: Union[int, str], quiet: bool = True) → str[source]

Look up an integer Autonomous System Number and return the human readable name of the organization.

Usage:

>>> asn_to_name(210083)
'PRIVEX, SE'
>>> asn_to_name('13335')
'CLOUDFLARENET - Cloudflare, Inc., US'

This helper function requires dnspython>=1.16.0, it will not be visible unless you install the dnspython package in your virtualenv, or systemwide:

pip3 install dnspython
Parameters
  • as_number (int/str) – The AS number as a string or integer, e.g. 210083 or ‘210083’

  • quiet (bool) – (default True) If True, returns ‘Unknown ASN’ if a lookup fails. If False, raises a KeyError if no results are found.

Raises

KeyError – Raised when a lookup returns no results, and quiet is set to False.

Return str as_name

The name and country code of the ASN, e.g. ‘PRIVEX, SE’

privex.helpers.net.ip4_to_rdns(ip_obj: ipaddress.IPv4Address, v4_boundary: int = 24, boundary: bool = False) → str[source]

Internal function for getting the rDNS domain for a given v4 address. Use ip_to_rdns() unless you have a specific need for this one.

Parameters
  • ip_obj (IPv4Address) – An IPv4 ip_address() object to get the rDNS domain for

  • v4_boundary (int) – 8-32 bits. If boundary is True, return the base rDNS domain at this boundary.

  • boundary (bool) – If True, cut off the rDNS domain to the given v4_boundary

Return str rdns_domain

in-addr.arpa format, e.g. 0.0.127.in-addr.arpa

privex.helpers.net.ip6_to_rdns(ip_obj: ipaddress.IPv6Address, v6_boundary: int = 32, boundary: bool = False) → str[source]

Internal function for getting the rDNS domain for a given v6 address. Use ip_to_rdns() unless you have a specific need for this one.

Parameters
  • ip_obj (IPv6Address) – An IPv4 ip_address() object to get the rDNS domain for

  • v4_boundary (int) – 8-128 bits. If boundary is True, return the base rDNS domain at this boundary.

  • boundary (bool) – If True, cut off the rDNS domain to the given v6_boundary

Return str rdns_domain

ip6.arpa format, e.g. 0.8.e.f.ip6.arpa

privex.helpers.net.ip_is_v4(ip: str) → bool[source]

Determines whether an IP address is IPv4 or not

Parameters

ip (str) – An IP address as a string, e.g. 192.168.1.1

Raises

ValueError – When the given IP address ip is invalid

Return bool

True if IPv6, False if not (i.e. probably IPv4)

privex.helpers.net.ip_is_v6(ip: str) → bool[source]

Determines whether an IP address is IPv6 or not

Parameters

ip (str) – An IP address as a string, e.g. 192.168.1.1

Raises

ValueError – When the given IP address ip is invalid

Return bool

True if IPv6, False if not (i.e. probably IPv4)

privex.helpers.net.ip_to_rdns(ip: str, boundary: bool = False, v6_boundary: int = 32, v4_boundary: int = 24) → str[source]

Converts an IPv4 or IPv6 address into an in-addr domain

Default boundaries: IPv4 - 24 bits, IPv6 - 32 bits

Examples:

>>> ip_to_rdns('127.0.0.1') # IPv4 to arpa format
    '1.0.0.127.in-addr.arpa'
>>> ip_to_rdns('2001:dead:beef::1') # IPv6 to arpa format
    '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.f.e.e.b.d.a.e.d.1.0.0.2.ip6.arpa'
>>> ip_to_rdns('2001:dead:beef::1', boundary=True) # IPv6 32-bit boundary to arpa
    'd.a.e.d.1.0.0.2.ip6.arpa'
Parameters
  • ip (str) – IPv4 or IPv6 address

  • boundary (bool) – If True, return the base (boundary) domain to place NS/SOA

  • v6_boundary (int) – Bits for IPv6 boundary. Must be dividable by 4 bits (nibble)

  • v4_boundary (int) – Bits for IPv4 boundary. Must be dividable by 8 bits (octet)

Raises
Return str rdns_domain

in-addr.arpa format, e.g. 0.0.127.in-addr.arpa

Return str rdns_domain

ip6.arpa format, e.g. 0.8.e.f.ip6.arpa