在做测试时需求一些列IP地址,需要一个函数生成大量的无规律的IP地址。下面有一种方法,所以生成IP地址,而且一行代码就解决了。

IPv4随机生成

1
2
3
4
5
6
7
import random

rip = lambda: '.'.join([str(int(''.join([str(random.randint(0, 2)), str(random.randint(0, 5)), str(random.randint(0, 5))]))) for _ in range(4)])

print(rip())
print(rip())

直观点表示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

import random

rip = lambda: '.'.join([str(int(''.join(
[str(random.randint(0, 2)),
str(random.randint(0, 5)),
str(random.randint(0, 5))]
)))
for _ in range(4)])

print(rip())
print(rip())

print([rip() for _ in range(100)])

测试下效果

1
2
3
4
5

133.10.33.44
54.223.50.11
['35.220.15.204', '32.113.43.141', '253.33.224.105', '211.13.120.245', '131.251.233.44', '22.254.131.224', '151.44.200.124', '2.40.242.230', '40.23.34.115', '244.53.125.55', '31.2.230.55', '110.31.45.214', '34.153.145.212', '210.112.154.112', '53.250.115.131', '114.20.155.41', '151.224.41.41', '115.2.24.241', '111.40.124.31', '53.2.240.4', '132.142.50.230', '11.154.113.1', '35.34.132.155', '101.203.45.23', '125.254.2.40', '243.221.225.115', '25.220.231.50', '55.55.223.45', '40.153.4.225', '250.140.3.135', '45.122.25.232', '13.205.135.11', '144.32.112.242', '251.223.135.11', '50.241.4.255', '233.40.121.201', '23.25.51.150', '215.11.3.53', '233.132.24.113', '110.125.131.25', '222.144.250.102', '10.212.115.200', '3.250.130.215', '2.34.251.23', '204.114.124.231', '30.53.112.151', '24.132.131.232', '245.5.243.103', '133.55.144.113', '0.224.24.134', '240.135.202.132', '120.210.251.232', '253.140.234.34', '114.113.204.203', '4.143.150.23', '15.102.103.143', '234.220.55.113', '32.24.154.234', '122.2.243.133', '25.120.14.124', '0.154.5.22', '151.115.134.245', '215.203.241.234', '35.202.44.154', '122.150.11.134', '35.203.120.55', '15.34.133.13', '122.140.255.202', '25.251.34.41', '14.202.40.55', '222.121.143.133', '134.104.152.13', '152.141.245.14', '100.113.11.135', '43.230.211.210', '211.154.43.43', '104.42.10.43', '122.30.32.132', '4.51.51.25', '144.230.110.52', '34.112.254.252', '151.42.201.240', '235.231.144.40', '125.120.254.250', '104.20.54.134', '30.142.200.150', '241.35.42.200', '24.235.201.203', '151.241.104.133', '22.132.23.21', '123.154.110.241', '123.122.211.203', '245.120.222.201', '113.13.40.143', '41.35.32.43', '100.145.232.34', '44.45.21.4', '54.25.250.105', '231.52.245.143', '240.241.135.141']

IPv6随机生成

类似地,如果要生产IPv6,根据IPv6的结构修改范围即可。

1
2
3
4
5
6
7
8
9
10
11
import random
import string

rip6 = lambda: ':'.join([''.join(
[random.choice(string.hexdigits[:16]) for _ in range(4)]
)
for _ in range(8)])

for _ in range(10):
print(rip6())

猜测效果

1
2
3
4
5
6
7
8
9
10
2fbe:2543:514c:a232:d10d:69e6:0a4c:6a9b
72c6:7e75:cb6c:37f8:a998:12ef:60aa:f5bd
d823:f4cc:6d80:d781:f560:204e:2222:f902
978d:79b8:4936:6dea:3223:b221:29c1:0718
6ca9:a4cf:9b21:fd7b:71e3:8093:11c1:619d
65d5:92a0:68b8:b3bd:84bb:933f:745d:7361
ec1c:7d63:241f:84b0:2508:baa3:c94f:c134
b80f:1fa5:6d8a:272f:85dd:8292:78ea:d530
496c:12c0:ee7c:c801:c1c9:8cc2:bbcb:7729
5777:20c5:9b2e:6cf8:ec01:b72d:3782:7021

过滤本地地址

如果只想生产global地址,可以使用ipaddress过滤。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

import ipaddress

# 示例

ip = ipaddress.IPv4Address('34.153.145.212')
print(ip.is_global) # True

# 嵌入到原来代码中
# 这里需要递归处理,如果生产的IP地址不是global类型,重新调用本身

def rip():
ip = '.'.join([str(int(''.join(
[str(random.randint(0, 2)),
str(random.randint(0, 5)),
str(random.randint(0, 5))]
)))
for _ in range(4)])
if ipaddress.IPv4Address(ip).is_global:
return ip
else:
return rip()

测试效果

1
2
3
4
5
6

45.53.23.43
113.152.43.1
['212.115.220.105', '235.242.242.12', '110.40.154.51', '55.243.223.202', '113.224.203.202', '112.231.52.43', '33.233.150.33', '231.51.41.14', '22.1.24.202', '13.31.131.115', '13.145.212.223', '111.253.255.34', '53.205.3.223', '124.145.20.145', '23.250.224.142', '55.12.225.225', '235.42.255.144', '210.15.101.123', '34.204.215.225', '221.212.234.101', '141.211.242.14', '152.255.55.11', '110.102.130.135', '141.250.235.140', '35.24.255.223', '20.101.122.233', '2.250.235.251', '5.235.10.245', '230.53.212.245', '155.221.221.255', '143.54.12.12', '221.201.45.114', '231.30.51.204', '204.234.45.31', '42.202.54.34', '115.221.201.151', '235.15.13.241', '202.113.212.235', '30.100.255.124', '153.234.223.54', '153.134.0.5', '233.145.223.102', '202.113.221.50', '121.30.134.102', '55.5.21.111', '33.115.13.34', '155.112.233.42', '143.115.43.241', '35.240.111.123', '101.110.202.21', '105.124.240.101', '222.220.132.103', '104.212.4.125', '232.233.124.154', '220.111.231.143', '133.134.21.102', '43.112.242.235', '201.42.132.204', '30.215.25.0', '43.235.21.1', '44.155.200.101', '43.35.104.144', '150.203.223.120', '53.43.155.12', '11.255.242.24', '100.145.142.130', '134.103.210.102', '202.243.123.211', '142.21.121.102', '140.131.214.42', '155.42.51.10', '234.230.135.233', '1.1.53.10', '210.2.240.115', '131.210.43.233', '31.251.215.52', '213.110.210.44', '43.112.114.124', '110.110.224.201', '212.22.103.203', '204.150.233.144', '212.223.221.225', '131.205.223.31', '221.12.231.204', '44.14.2.34', '203.251.155.30', '54.244.214.154', '115.105.144.110', '202.55.25.231', '4.53.110.54', '152.111.15.152', '210.144.32.253', '105.110.230.135', '135.24.112.123', '45.103.41.113', '134.33.100.202', '202.141.221.230', '45.34.34.121', '31.131.1.100', '40.233.224.43']


引入ipaddress

如果引入ipaddress模块,还有更简单的方法生产随机IP地址。IPv4是32位地址,每一个地址都可以用一整数表示。范围从0-2**32-1(4294967296-1)

1
2
3
4
5
import random
import ipaddress

rip4 = lambda: ipaddress.IPv4Address(random.randint(0, 2**32-1)).exploded

类似地,IPv6是128位地址。

1
2
3
4
5
6
7
8
import random
import ipaddress

rip6 = lambda: ipaddress.IPv6Address(random.randint(0, 2**128-1)).exploded

for _ in range(10):
print(rip6())

1
2
3
4
5
6
7
8
9
10
11
12

e020:210a:c270:06ca:79ab:4339:403c:6576
93e8:ff95:8851:af5d:9a54:f91e:eb31:7a1b
6ba7:bcdf:c301:f7e2:6e5f:491c:17a9:ecd2
f807:efb3:933f:bcee:fdad:f0eb:9ad8:ba07
67d4:e36d:72b9:8257:430d:e6bd:b982:c885
e0f7:261a:5fe9:1d66:5fed:7cb7:8403:88b1
59f1:733b:ee63:0831:8650:277b:625d:7924
40e3:47c6:60c9:8683:01b0:e6b9:2707:23ea
e04e:9790:5ab5:dd36:b917:9821:b503:acf8
25fb:bade:60a3:f6db:788c:d2e2:1e54:5af2

生成CIDR地址对应的IP地址集

CIDR地址形如“192.168.23.23/16”,我们想把它转换为对应的IP地址集,

1
2
3
4
import ipaddress
net = ipaddress.ip_network('223.33.66.64/27')
ips = [str(ip) for ip in net]
print(ips)

类似地,IPv6也如此。

转载请包括本文地址:https://allenwind.github.io/blog/2171
更多文章请参考:https://allenwind.github.io/blog/archives/