4 # Id: gen-data-queryperf.py,v 1.2 2008/06/13 18:17:08 jinmei Exp
6 # Contributed by Stephane Bortzmeyer <bortzmeyer@nic.fr>
8 # "A small tool which may be useful with contrib/queryperf. This script
9 # can generate files of queries, both with random names (to test the
10 # behaviour with NXdomain) and with domains from a real zone file."
20 for i
in range(97, 122):
23 for i
in range(48, 57):
35 domain_ns
= r
'^([a-z0-9-\.]+)((\s+\d+)?(\s+IN)?|(\s+IN)(\s+\d+)?)\s+NS'
36 domain_ns_re
= re
.compile(domain_ns
, re
.IGNORECASE
)
38 def remove_tld(label
, tld
):
39 if label
.endswith('.' + tld
+ '.'):
40 return label
[0:-(1+ len(tld
) + 1)]
44 def gen_random_label():
46 for i
in range(gen
.randint(1, maxsize
)):
47 label
= label
+ gen
.choice(ldh
)
50 def make_domain(label
):
51 return "www." + label
+ "." + tld
+ " A"
54 sys
.stdout
.write("Usage: " + sys
.argv
[0] + " [-n number] " + \
55 "[-p percent-random] [-t TLD]\n")
56 sys
.stdout
.write(" [-m MAXSIZE] [-f zone-file]\n")
59 optlist
, args
= getopt
.getopt(sys
.argv
[1:], "hp:f:n:t:m:",
60 ["help", "percentrandom=", "zonefile=",
63 for option
, value
in optlist
:
64 if option
== "--help" or option
== "-h":
67 elif option
== "--number" or option
== "-n":
69 elif option
== "--maxsize" or option
== "-m":
71 elif option
== "--percentrandom" or option
== "-p":
72 percent_random
= float(value
)
73 elif option
== "--tld" or option
== "-t":
75 elif option
== "--zonefile" or option
== "-f":
76 zone_file
= str(value
)
78 error("Unknown option " + option
)
79 except getopt
.error
, reason
:
80 sys
.stderr
.write(sys
.argv
[0] + ": " + str(reason
) + "\n")
90 file = open(zone_file
)
91 line
= file.readline()
93 domain_line
= domain_ns_re
.match(line
)
95 print domain_line
.group(1)
96 domain
= remove_tld(domain_line
.group(1), tld
)
98 line
= file.readline()
101 domains
= domains
.keys()
102 if len(domains
) == 0:
103 sys
.stderr
.write("No domains found in '%s'\n" % zone_file
)
107 if gen
.random() < percent_random
:
108 sys
.stdout
.write(make_domain(gen_random_label()))
110 sys
.stdout
.write(make_domain(gen
.choice(domains
)))
112 sys
.stdout
.write(make_domain(gen_random_label()))
113 sys
.stdout
.write("\n")