1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import string
import socket
import struct
import math
import re
import subprocess
import glob
def alnum_gen(r, l=1):
s = u""
for i in xrange(l):
s += r.choice(string.ascii_letters + string.digits)
return s
def fortune(r, max_len):
all_fortunes = []
for fortune_file in glob.glob('/usr/share/games/fortunes/*.u8'):
f = open(fortune_file)
l = f.read().split('\n%\n')[:-1]
for i in l:
if len(i) < max_len:
all_fortunes.append(i)
return re.sub(r'\s+', ' ', r.choice(all_fortunes)).strip()
def _net_to_int(s):
try:
net, subnet = s.split('/')
except ValueError:
subnet = '32'
try:
subnet_int = int(subnet)
except ValueError:
subnet_bytes = struct.unpack('>I', socket.inet_aton(subnet))[0]
max_bit = 1 << 31
subnet_int = 0
while (subnet_bytes & max_bit) > 0:
subnet_int += 1
max_bit >>= 1
return struct.unpack('>I', socket.inet_aton(net))[0], subnet_int
def IPv4_subnet_gen(r, base_net, mask = 24):
base_addr, base_subnet = _net_to_int(base_net)
a = r.randint(1, 1 << mask - base_subnet) << (32 - mask)
if a >= 1<<32:
a = 0
net_addr = base_addr | a
return socket.inet_ntoa(struct.pack('>I', net_addr)) + '/{0}'.format(mask)
def IPv4_net_gen(r, min_hosts=254, local=True, multicast=False):
mask = 32 - int(math.ceil(math.log(min_hosts, 2)))
if local and not multicast:
net = r.choice([
"192.168.0.0/16",
'10.0.0.0/8',
'172.16.0.0/12'])
if multicast:
if local:
net = "239.0.0.0/8"
else:
net = "224.0.0.0/4"
return IPv4_subnet_gen(r, net, mask)
def IPv4_addr_gen(r, network, n_generated=1):
net, mask = _net_to_int(network)
hosts = []
l = r.sample(xrange(1, 2**(32 - mask)-1), n_generated)
for i in l:
hosts.append(socket.inet_ntoa(struct.pack('>I', net | i)))
return hosts
def MAC_gen(r):
s = "0123456789ABCDEF"
return ":".join([r.choice(s) + r.choice("26AE")] + \
[r.choice(s) + r.choice(s) for i in xrange(5)])
common_file_extensions = ['jpg', 'png', 'txt', 'doc', 'cfg', 'pdf', 'odt', 'cpp', 'c', 'sh', 'java']
def fname_gen(r, extension = True):
s = alnum_gen(r, 8)
if extension:
s += '.' + r.choice(common_file_extensions)
return s
with open('random_data/greek_gods.txt') as f:
greek_gods = [i.strip() for i in f.readlines()]
with open('random_data/roman_gods.txt') as f:
roman_gods = [i.strip() for i in f.readlines()]
with open('random_data/slavic_gods.txt') as f:
slavic_gods = [i.strip() for i in f.readlines()]
gods = greek_gods + roman_gods + slavic_gods
def hostname_gen(r):
return "{0}-{1:02}".format(r.choice(gods), r.randint(1, 99))
with open('random_data/slovenian_names.txt') as f:
names = [i.strip() for i in f.readlines()]
with open('random_data/slovenian_surnames.txt') as f:
surnames = [i.strip() for i in f.readlines()]
def username_gen(r):
return r.choice(names) + r.choice(surnames) + "{0}".format(r.randint(1, 99))
def unknown_generator(r):
return ''
default_generators = {
'IP': lambda r: IPv4_addr_gen(r, IPv4_net_gen(r))[0],
'localnet': lambda r: IPv4_net_gen(r, min_hosts = r.randint(16, 250), local=True, multicast=False),
'filename': fname_gen,
'dirname': lambda r: fname_gen(r, extension = False),
'username': username_gen,
'password': lambda r: alnum_gen(r, 8),
'short_text': lambda r: fortune(r, 40),
'hostname': lambda r: hostname_gen(r),
None: lambda r: alnum_gen(r, 8),
'': unknown_generator,
}
def default_gen(userID, param_meta):
r = random.Random(userID)
params = dict()
for name, meta in param_meta.iteritems():
if meta.get('generated', False):
params[name] = default_generators(
meta.get('type', None), unknown_generator)(r)
return params
if __name__ == '__main__':
r = random.Random()
for k, v in default_generators.iteritems():
print ("---{}---".format(k))
print (v(r))
|