inetnum.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:enteletaor 作者: cr0hn 项目源码 文件源码
def get_inet_num(search_term):
    """
    Get intetnums for a domain

    :param search_term: keywork without dots, no domains are allowed. domain.com -> invalid |---| domain -> valid
    :type search_term: str

    :return: iterable with IP/CIDR notation or None 
    :rtype: list(str) | None

    """
    # Disable request logging
    requests_log = logging.getLogger("requests")
    requests_log.addHandler(logging.NullHandler())
    requests_log.propagate = False

    # Search the RIPE database
    # There is an issue with RIPE. When making a request and including
    # the type-filter inetnum, the JSON response also includes other types.
    request = requests.get('http://rest.db.ripe.net/search.json', params={
        'query-string': search_term,
        'type-filter': 'inetnum'
    })

    json_results = json.loads(request.text)

    try:
        # Filter any object that doesn't have the type 'inetnum'
        ranges = [x['primary-key']['attribute'][0]['value']
                  for x in json_results['objects']['object']
                  if x['type'] == 'inetnum']
    except KeyError:
        return None

    # Turn the IP range string into CIDR
    cidrs = []
    for _range in ranges:
        _range = _range.split(' - ');
        cidrs.append(netaddr.iprange_to_cidrs(_range[0], _range[1]))

    results = set()

    # Print the CIDR's
    for cidr in cidrs:
        results.add(str(cidr[0]))

    return results
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号