Skip to content

Instantly share code, notes, and snippets.

@xanderificnl
Forked from wido/random-ipv6-addr.py
Last active September 26, 2022 04:17
Show Gist options
  • Save xanderificnl/80b6a464bf2dca34ccad2fa564e346ab to your computer and use it in GitHub Desktop.
Save xanderificnl/80b6a464bf2dca34ccad2fa564e346ab to your computer and use it in GitHub Desktop.
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 = u'2a02:58:143:e00::/56'
seed()
network = IPv6Network(subnet)
address = IPv6Address(network.network_address + getrandbits(network.max_prefixlen - network.prefixlen))
print(address)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment