Skip to content

Instantly share code, notes, and snippets.

@chungy
Created August 10, 2021 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chungy/fe21c76a1d449a868c0504332bca3820 to your computer and use it in GitHub Desktop.
Save chungy/fe21c76a1d449a868c0504332bca3820 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from ipaddress import IPv4Address, IPv6Address
from random import randint
# Unicast MAC addresses can’t have an odd first octet. Also generate
# a whole bunch just to improve use in making a whole VM setup.
mac_mask = 0xFE_FF_FF_FF_FF_FF
print("MAC addresses:")
for i in range(0, 10):
mac = mac_mask & randint(0, 2 ** 48 - 1)
print(
":".join(
[
"{:02X}".format((mac >> octet) & 0xFF)
for octet in range(0, 48, 8)
][::-1]
)
)
# Local IPv4 networks are in 10/8, 172.16/12, and 192.168/16
print("\nIPv4 /24s:")
print(IPv4Address((0xA << 24 | randint(0, 2 ** 16 - 1) << 8)))
print(IPv4Address((0xAC1 << 20 | randint(0, 2 ** 12 - 1) << 8)))
print(IPv4Address((0xC0A8 << 16 | randint(0, 2 ** 8 - 1) << 8)))
# Local IPv6 networks are in fc00::/7.
# To get a /64, randomize the other 57 bits.
print("\nIPv6 /64:")
print(IPv6Address((0xFC00 << 112 | randint(0, 2 ** 57 - 1) << 64)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment