-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneratehosts
executable file
·221 lines (175 loc) · 5.54 KB
/
generatehosts
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/perl
#
# generatehosts - utility to add host records
#
# Copyright (c) Michal Kostenec <[email protected]> 2013-2014.
# Copyright (c) Timo Kokkonen <[email protected]> 2001-2004.
#
require 5;
use Getopt::Long;
use Net::Netmask;
use Sauron::DB;
use Sauron::Util;
use Sauron::BackEnd;
use Sauron::Sauron;
use Net::IP qw(:PROC);
load_config();
#######################################################################
sub tag_expand($) {
my($str) = @_;
my($i2,$i3,$i4,$h1,$h2,$h3,$h4,$ip1,$ip2,$ip3,$ip4);
return '' if ($str =~ /^\s*$/);
$i1=$i+$seq_start;
$i2=sprintf("%02d",$i1);
$i3=sprintf("%03d",$i1);
$i4=sprintf("%04d",$i1);
$h1=sprintf("%x",$i1);
$h2=sprintf("%02x",$i1);
$h3=sprintf("%03x",$i1);
$h4=sprintf("%04x",$i1);
if ($ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) {
$ip1=sprintf("%03d",$1);
$ip2=sprintf("%03d",$2);
$ip3=sprintf("%03d",$3);
$ip4=sprintf("%03d",$4);
$str=~ s/:IP1:/$ip1/g;
$str=~ s/:IP2:/$ip2/g;
$str=~ s/:IP3:/$ip3/g;
$str=~ s/:IP4:/$ip4/g;
}
elsif ($ip =~ /^([0-9a-fA-F]{4}\:){4}([0-9a-fA-F]{4})\:([0-9a-fA-F]{4})\:([0-9a-fA-F]{4})\:([0-9a-fA-F]{4})$/) {
$ip1=sprintf("%04s",$2);
$ip2=sprintf("%04s",$3);
$ip3=sprintf("%04s",$4);
$ip4=sprintf("%04s",$5);
$str=~ s/:IP1:/$ip1/g;
$str=~ s/:IP2:/$ip2/g;
$str=~ s/:IP3:/$ip3/g;
$str=~ s/:IP4:/$ip4/g;
}
$str =~ s/:N:/$i1/g;
$str =~ s/:N2:/$i2/g;
$str =~ s/:N3:/$i3/g;
$str =~ s/:N4:/$i4/g;
$str =~ s/:H:/$h1/g;
$str =~ s/:H2:/$h2/g;
$str =~ s/:H3:/$h3/g;
$str =~ s/:H4:/$h4/g;
return $str;
}
######################################################################
$user = (getpwuid($<))[0];
set_muser($user);
GetOptions("help|h","name=s","commit","noreverse","seqstart=s",
"outofzone","verbose","group=s","info=s");
if ($opt_help || @ARGV < 5) {
print "syntax: $0 <server> <zone> <hostname> <starting-IP> <count>\n\n",
"options:\n",
"\t--noreverse\t\tdo not add reverse records\n",
"\t--outofzone\t\tallow out of zone records\n",
"\t--verbose\t\tproduce more verbose output\n",
"\t--seqstart=<n>\t\tstart sequence numbering from n\n",
"\t--group=<name>\t\tassign new hosts to given group\n",
"\t--info=<user>:<dept>:<location>:<extra>\n\n",
"\t--commit\t\tcommit changes (w/o this no changes are made)\n",
"IPv4 tags:\n",
"\t:IP1:\t\tfirst octet of current IP-number\n",
"\t:IP2:\t\tsecond octet of current IP-number\n",
"\t:IP3:\t\tthird octet of current IP-number\n",
"\t:IP4:\t\tfourth octet of current IP-number\n",
"\n",
"IPv6 tags:\n",
"\t:IP1:\t\tfifth hextet of current IP-number\n",
"\t:IP2:\t\tsixth hextet of current IP-number\n",
"\t:IP3:\t\tseventh hextet of current IP-number\n",
"\t:IP4:\t\teighth hextet of current IP-number\n",
"\n",
"Common tags:\n",
"\t:N:\t\tsequence number\n",
"\t:N2:\t\tsequence number\n",
"\t:N3:\t\tsequence number\n",
"\t:N4:\t\tsequence number\n",
"\t:H:\t\tsequence number in hexa\n",
"\t:H2:\t\tsequence number in hexa\n",
"\t:H3:\t\tsequence number in hexa\n",
"\t:H4:\t\tsequence number in hexa\n",
"\n";
print "" if ($opt_help);
exit(0);
}
$opt_commit = ($opt_commit ? 1 : 0);
$opt_noreverse = ($opt_noreverse ? 1 : 0);
$server=$ARGV[0];
$zone=$ARGV[1];
$hostname=$ARGV[2];
$startip=$ARGV[3];
$count=$ARGV[4];
$gid=-1;
$seq_start=0;
$add_count=0;
db_connect();
if ($opt_seqstart) {
$seq_start = $opt_seqstart;
fatal("invalid parameter to 'seq-start'") unless ($seq_start =~ /^\d+$/);
}
$serverid=get_server_id($server);
fatal("cannot find server '$server'") unless ($serverid > 0);
$zoneid=get_zone_id($zone,$serverid);
fatal("cannot find zone '$zone'") unless ($zoneid > 0);
if ($opt_group) {
fatal("cannot find group '$opt_group'")
if (($gid=get_group_by_name($serverid,$opt_group)) < 0);
}
if ($opt_info) {
@infof = split(/:/,$opt_info);
fatal("invalid paremeters for info option ") if (@infof > 4);
}
fatal("invalid 'count' parameter: $count") unless ($count > 0);
fatal("invalid 'hostname' parameter: '$hostname'")
unless ($hostname =~ /^\S+$/);
fatal("invalid 'starting-IP' parameter: $startip") unless (is_cidr($startip));
my $range = new Net::IP("$startip + " . ($count - 1)) or fatal("cannot create range variable");
$origin=$zone;
$origin.="." unless ($zone =~ /\.$/);
my $family = (ip_is_ipv4($startip) ? 4 : 6);
print "IP range: " . $startip . " - ". ip_compress_address($range->last_ip(), $family) . "\n" if ($opt_verbose);
db_begin();
db_ignore_begin_and_commit(1);
$i = 0;
do {
$ip = $range->ip();
$ip_c = ip_compress_address($range->ip(), $family);
$domain=tag_expand($hostname);
$host=remove_origin($domain,$origin);
print "$host : " . $ip_c . " \n";
undef @q;
db_query("SELECT id FROM hosts WHERE zone=$zoneid AND domain = '$host';",
\@q);
if (defined $q[0][0] and ($id=$q[0][0]) > 0) {
fatal("host: $host already exists!");
}
undef %host;
$host{domain}=$host;
$host{type}=1;
$host{zone}=$zoneid;
$host{grp}=$gid;
$host{ip}=[[0,$ip_c,'t',($opt_noreverse ? 'f' : 't')]];
$host{huser}=tag_expand($infof[0]) if ($infof[0]);
$host{dept}=tag_expand($infof[1]) if($infof[1]);
$host{location}=tag_expand($infof[2]) if($infof[2]);
$host{info}=tag_expand($infof[3]) if($infof[3]);
$id=add_host(\%host);
fatal("cannot insert host_record! $id") unless ($id > 0);
$add_count++;
$i++;
} while (++$range);
db_ignore_begin_and_commit(0);
print "$add_count host(s) added.\n" if ($opt_verbose);
if ($opt_commit) {
fatal("cannot commit changes to database") if (db_commit() < 0);
} else {
db_rollback();
print "NO CHANGES MADE!\n";
}
exit;
# eof :-)