#!/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 += random.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 = random.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 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)) 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), } 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)](r) return params if __name__ == '__main__': r = random.Random() for k, v in default_generators.iteritems(): print "---", k, "---" print v(r)