2

在我的测试用例中,我需要发送带有随机 IPv6 源地址和固定前缀的 NA。例如:

固定前缀 2001::cafe:/64。地址的其余部分应该是随机的。

如何在 Python 或 Scapy 中实现?

4

4 回答 4

6
import random   
M = 16**4
"2001:cafe:" + ":".join(("%x" % random.randint(0, M) for i in range(6)))
于 2011-10-05T12:00:44.460 回答
5

从问题中不确定您希望地址的哪些部分是随机的。我假设最后 2 个字节。

使用 python netaddr库:

import random
from netaddr.ip import IPNetwork, IPAddress

random.seed()
ip_a = IPAddress('2001::cafe:0') + random.getrandbits(16)
ip_n = IPNetwork(ip_a)
ip_n.prefixlen = 64

print ip_a
print ip_n

样本输出:

2001::cafe:c935
2001::cafe:c935/64

与简单的字符串格式化相比,它的优点是可以很容易地自定义起始地址,随机位 len。netaddr 类也有许多有用的属性,例如网络的广播地址。

于 2011-10-05T11:56:41.363 回答
0

Scapy has a function built-in to generate random ipv6 addresses.

source = RandIP6("2000::cafe:*:*")
print source

Note that, if its messing around with zero's , you need to add 2 lines in the broncode of scapy/volatile.py before line 286

if n == 0:
        ip.append("0")
于 2013-11-21T13:34:12.530 回答
0

仅使用字符串格式:

import random

random.seed()
print '2001::cafe:%x/64' % random.getrandbits(16)
于 2011-10-05T12:01:23.420 回答