如何生成随机ipv4、ipv6地址和MAC地址

借助Python的fake库的internet包

>>> from faker import Faker
>>> from faker.providers import internet
>>> fake=Faker()

  • 生成ipv4地址

带掩码长度

>>> fake.ipv4(network=True)
'192.53.63.160/28'
不带掩码长度
>>> fake.ipv4(network=False)
'4.129.77.8'

还可以生成私网地址、公网地址、某个类型的地址,例如

>>> fake.ipv4(network=True, address_class='a', private=None)
'100.192.0.0/12'
>>> fake.ipv4(network=True, address_class='b', private=None)
'172.0.0.0/6'
>>> fake.ipv4(network=True, address_class='c', private=None)
'203.0.51.128/27'

  • 生成IPv6地址

带掩码长度

>>> fake.ipv6(network=True)
'ced5:5cba:9206::/48'
不带掩码长度
>>> fake.ipv6(network=True)
'aba8:f04:e12c:e0aa:b967:f4bf:481c:d400/119'

  • 生成MAC地址

>>> fake.mac_address()
u'71:92:6a:b2:4f:b2'
>>> fake.mac_address()
u'ab:1a:ff:1c:cc:6a'

参考链接 https://pypi.org/project/Faker/

https://faker.readthedocs.io/en/stable/providers/faker.providers.internet.html

你可能感兴趣的:(Python)