Skip to content

Instantly share code, notes, and snippets.

@hatarist
Created May 2, 2018 13:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hatarist/1347a4eb9283f985417721e604e1fdab to your computer and use it in GitHub Desktop.
Save hatarist/1347a4eb9283f985417721e604e1fdab to your computer and use it in GitHub Desktop.
I'm a lazy ass who didn't think it's worth to bother with the standard ipaddress library
import random
import sys
from netaddr import IPNetwork, IPAddress
def generate_random_ipv6(subnet):
network = IPNetwork(subnet)
return str(IPAddress(random.randrange(network.first, network.last)))
if __name__ == '__main__':
if len(sys.argv) not in (2, 3):
print('Usage: python ipv6gen.py <subnet> <amount>')
print('Example:')
print(' python ipv6gen.py 4001:266:f088:1acd::1/64 15')
print(' (shows a list of 15 random IPs within the given subnet)')
sys.exit(1)
subnet = sys.argv[1]
amount = int(sys.argv[2]) if len(sys.argv) == 3 else 10
for _ in range(amount):
print(generate_random_ipv6(subnet))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment