mirror of
https://github.com/secynic/ipwhois.git
synced 2025-12-10 00:40:57 -06:00
Added new generators to utils.py: ipv4_generate_random, ipv6_generate_random (#183)
This commit is contained in:
parent
87e9d158a6
commit
f2094788ed
@ -17,6 +17,8 @@ Changelog
|
|||||||
modified ASN whois lookups. New argument get_asn_description to disable
|
modified ASN whois lookups. New argument get_asn_description to disable
|
||||||
additional DNS lookup (#176)
|
additional DNS lookup (#176)
|
||||||
- Fixed some test function naming errors
|
- Fixed some test function naming errors
|
||||||
|
- Added new generators to utils.py: ipv4_generate_random and
|
||||||
|
ipv6_generate_random (#183)
|
||||||
|
|
||||||
0.15.1 (2017-02-16)
|
0.15.1 (2017-02-16)
|
||||||
-------------------
|
-------------------
|
||||||
|
|||||||
37
UTILS.rst
37
UTILS.rst
@ -129,3 +129,40 @@ addresses/networks. Summarizes ports with sub-counts.
|
|||||||
'74.125.0.0/16': {'count': 1, 'ports': {}},
|
'74.125.0.0/16': {'count': 1, 'ports': {}},
|
||||||
'74.125.225.229': {'count': 2, 'ports': {'80': 1}}}
|
'74.125.225.229': {'count': 2, 'ports': {'80': 1}}}
|
||||||
|
|
||||||
|
Generate random IP addresses
|
||||||
|
----------------------------
|
||||||
|
Generate random, unique IPv4/IPv6 addresses that are not defined (can be
|
||||||
|
looked up using ipwhois).
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
>>>> from ipwhois.utils import ipv4_generate_random
|
||||||
|
>>>> for address in ipv4_generate_random(10):
|
||||||
|
>>>> print(address)
|
||||||
|
|
||||||
|
71.58.89.10
|
||||||
|
17.206.180.200
|
||||||
|
156.94.166.94
|
||||||
|
36.92.169.70
|
||||||
|
52.214.0.208
|
||||||
|
174.254.156.179
|
||||||
|
33.184.228.52
|
||||||
|
17.58.3.61
|
||||||
|
101.151.158.16
|
||||||
|
61.162.38.154
|
||||||
|
|
||||||
|
>>>> from ipwhois.utils import ipv6_generate_random
|
||||||
|
>>>> for address in ipv6_generate_random(10):
|
||||||
|
>>>> print(address)
|
||||||
|
|
||||||
|
218e:a9ad:aae4:431c:ff16:eb94:f063:47f7
|
||||||
|
24ba:3185:a26f:fd30:5756:16d5:b4ab:771b
|
||||||
|
38ad:f797:360a:d98e:4f3b:b1c8:5811:8425
|
||||||
|
2c0e:9add:6b48:96c4:d22:2674:8067:2de9
|
||||||
|
3b72:414b:c387:4650:c4a6:eed3:21a8:ba9b
|
||||||
|
3d24:4053:dd81:d269:2cdc:91c9:b0f8:830e
|
||||||
|
32a4:8ef8:807:1bf0:e866:c8d7:d69e:2a52
|
||||||
|
2a2b:eb87:d368:89ee:6861:555:32c6:d552
|
||||||
|
2ee6:5445:f1ff:b1c6:d68f:3ee1:1e31:fe34
|
||||||
|
2c6b:393f:ae7:a0f7:1c2:2e19:bab1:af9c
|
||||||
|
|
||||||
|
|||||||
@ -8,7 +8,9 @@ from ipwhois.utils import (ipv4_lstrip_zeros,
|
|||||||
ipv4_is_defined,
|
ipv4_is_defined,
|
||||||
ipv6_is_defined,
|
ipv6_is_defined,
|
||||||
unique_everseen,
|
unique_everseen,
|
||||||
unique_addresses)
|
unique_addresses,
|
||||||
|
ipv4_generate_random,
|
||||||
|
ipv6_generate_random)
|
||||||
|
|
||||||
LOG_FORMAT = ('[%(asctime)s] [%(levelname)s] [%(filename)s:%(lineno)s] '
|
LOG_FORMAT = ('[%(asctime)s] [%(levelname)s] [%(filename)s:%(lineno)s] '
|
||||||
'[%(funcName)s()] %(message)s')
|
'[%(funcName)s()] %(message)s')
|
||||||
@ -255,4 +257,18 @@ class TestFunctions(TestCommon):
|
|||||||
'count': 1, 'ports': {}}}
|
'count': 1, 'ports': {}}}
|
||||||
|
|
||||||
self.assertEqual(unique_addresses(file_path=fp),
|
self.assertEqual(unique_addresses(file_path=fp),
|
||||||
fp_expected_result)
|
fp_expected_result)
|
||||||
|
|
||||||
|
def test_ipv4_generate_random(self):
|
||||||
|
|
||||||
|
self.assertEquals(len(list(ipv4_generate_random(1000))), 1000)
|
||||||
|
|
||||||
|
for address in ipv4_generate_random(10):
|
||||||
|
print(address)
|
||||||
|
|
||||||
|
def test_ipv6_generate_random(self):
|
||||||
|
|
||||||
|
self.assertEquals(len(list(ipv6_generate_random(1000))), 1000)
|
||||||
|
|
||||||
|
for address in ipv6_generate_random(10):
|
||||||
|
print(address)
|
||||||
|
|||||||
@ -29,6 +29,7 @@ import re
|
|||||||
import copy
|
import copy
|
||||||
import io
|
import io
|
||||||
import csv
|
import csv
|
||||||
|
import random
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
if sys.version_info >= (3, 3): # pragma: no cover
|
if sys.version_info >= (3, 3): # pragma: no cover
|
||||||
@ -557,3 +558,53 @@ def unique_addresses(data=None, file_path=None):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def ipv4_generate_random(total=100):
|
||||||
|
"""
|
||||||
|
The generator to produce random, unique IPv4 addresses that are not
|
||||||
|
defined (can be looked up using ipwhois).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
total: The total number of IPv4 addresses to generate.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Generator: Yields a generator object.
|
||||||
|
"""
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
yielded = set()
|
||||||
|
while count < total:
|
||||||
|
|
||||||
|
address = str(IPv4Address(random.randint(0, 2**32-1)))
|
||||||
|
|
||||||
|
if not ipv4_is_defined(address)[0] and address not in yielded:
|
||||||
|
|
||||||
|
count += 1
|
||||||
|
yielded.add(address)
|
||||||
|
yield address
|
||||||
|
|
||||||
|
|
||||||
|
def ipv6_generate_random(total=100):
|
||||||
|
"""
|
||||||
|
The generator to produce random, unique IPv6 addresses that are not
|
||||||
|
defined (can be looked up using ipwhois).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
total: The total number of IPv6 addresses to generate.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Generator: Yields a generator object.
|
||||||
|
"""
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
yielded = set()
|
||||||
|
while count < total:
|
||||||
|
|
||||||
|
address = str(IPv6Address(random.randint(0, 2**128-1)))
|
||||||
|
|
||||||
|
if not ipv6_is_defined(address)[0] and address not in yielded:
|
||||||
|
|
||||||
|
count += 1
|
||||||
|
yielded.add(address)
|
||||||
|
yield address
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user