Skip to content

Instantly share code, notes, and snippets.

@wido
Last active November 28, 2022 14:33
Embed
What would you like to do?
Generate a random IPv6 address
#!/usr/bin/env python3
"""
Generate a random IPv6 address for a specified subnet
"""
from random import seed, getrandbits
from ipaddress import IPv6Network, IPv6Address
subnet = '2001:db8:100::/64'
seed()
network = IPv6Network(subnet)
address = IPv6Address(network.network_address + getrandbits(network.max_prefixlen - network.prefixlen))
print(address)
@robetus
Copy link

robetus commented Sep 5, 2017

I needed to write it like this to get it to work:

subnet = u'2001:db8:100::/64'

Can you create it to make 1 random IPv6 address from multiple different IPv6 subnets?

@arya6000
Copy link

arya6000 commented Sep 27, 2017

It would be very useful if this is modified to give X amount of IPv6 IPs? for example it would generate 1000 IPv6 IPs starting from the lowest range. or does such script already exist?

@json-m
Copy link

json-m commented Oct 23, 2018

It would be very useful if this is modified to give X amount of IPv6 IPs? for example it would generate 1000 IPv6 IPs starting from the lowest range. or does such script already exist?

$ for ip in {1..1000}; do python random-ipv6-addr.py ; done ? not exactly what you asked for but you can work from there..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment