-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns2phpipam
executable file
·385 lines (292 loc) · 10.3 KB
/
dns2phpipam
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env php
<?php
// -----------------------------------------------------------------------------
// Usage: $ ./dns2phpipam [-test] <subnet>
//
// Example:
// $ ./dns2phpipam -test 147.251.90.0/24
// $ ./dns2phpipam 147.251.90.0/24
// -----------------------------------------------------------------------------
printf("\n");
// subnet is the first argument
if( (count($argv) != 2) && (count($argv) != 3) ){
printf(">>> ERROR: Subnet not specified!\n\n");
exit(1);
}
if( $argv[1] == "-test" ){
$subnet = $argv[2];
if( $subnet == "" ) {
printf(">>> ERROR: Subnet not specified!\n\n");
exit(1);
}
$testmode = true;
printf("SubNet: %s\n",$subnet);
printf("Test : ON\n");
} else {
$subnet = $argv[1];
if( $subnet == "" ) {
printf(">>> ERROR: Subnet not specified!\n\n");
exit(1);
}
$testmode = false;
printf("SubNet: %s\n",$subnet);
printf("Test : OFF - changes will be commited to IPAM\n");
}
// access to phpIPAM api
include 'phpipam.conf';
// -----------------------------------------------------------------------------
$subnetid = 0;
$data = [];
$token = "";
$min_host = "";
$max_host = "";
// -----------------------------------------------------------------------------
function init_session()
{
global $url, $username, $passwd, $token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "user/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$passwd");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
if( $server_output == FALSE ){
printf(">>> ERROR: Unable to connect to IPAM: %s\n\n",curl_error($ch));
curl_close ($ch);
exit(1);
}
curl_close ($ch);
$json = json_decode($server_output);
$token = $json->data->token;
//printf("Session token: %s\n",$token);
}
// -----------------------------------------------------------------------------
function get_subnetid()
{
global $url, $token, $subnetid, $subnet;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "subnets/cidr/" . $subnet ."/");
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
"token: $token"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
if( $server_output == FALSE ){
printf(">>> ERROR: Unable to connect to IPAM: %s\n\n",curl_error($ch));
curl_close ($ch);
exit(1);
}
curl_close ($ch);
$subnet_data = json_decode($server_output);
if( $subnet_data->success == false ){
printf(">>> ERROR: Unable to get subnet id for %s\n\n",$subnet);
var_dump($subnet_data);
exit(1);
}
$subnetid = $subnet_data->data[0]->id;
}
// -----------------------------------------------------------------------------
function get_subnetrange()
{
global $url, $token, $subnetid, $subnet, $min_host, $max_host;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "subnets/" . $subnetid ."/");
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
"token: $token"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
if( $server_output == FALSE ){
printf(">>> ERROR: Unable to connect to IPAM: %s\n\n",curl_error($ch));
curl_close ($ch);
exit(1);
}
curl_close ($ch);
$subnet_data = json_decode($server_output);
if( $subnet_data->success == false ){
printf(">>> ERROR: Unable to get hosts for %s\n\n",$subnet);
var_dump($subnet_data);
exit(1);
}
$min_host = $subnet_data->data->calculation->{'Min host IP'};
$max_host = $subnet_data->data->calculation->{'Max host IP'};
}
// -----------------------------------------------------------------------------
function get_hosts()
{
global $url, $token, $subnetid, $subnet, $data;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "subnets/" . $subnetid ."/addresses/");
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
"token: $token"
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
if( $server_output == FALSE ){
printf(">>> ERROR: Unable to connect to IPAM: %s\n\n",curl_error($ch));
curl_close ($ch);
exit(1);
}
curl_close ($ch);
$hosts = json_decode($server_output);
if( $hosts->success == false ){
printf(">>> ERROR: Unable to get hosts for %s\n\n",$subnet);
var_dump($hosts);
exit(1);
}
foreach($hosts->data as $host){
$ip = trim($host->ip);
$id = $host->id;
$item = [
'id' => $id,
'oldname' => $host->hostname,
'newname' => '',
];
$data[$ip] = $item;
}
}
// -----------------------------------------------------------------------------
function scan_dns()
{
global $data, $min_host, $max_host;
$min = explode(".",$min_host)[3];
$max = explode(".",$max_host)[3];
for($i = $min; $i <= $max; $i++){
$ip = explode(".",$min_host)[0] . "." . explode(".",$min_host)[1] . "." . explode(".",$min_host)[2] . "." . $i;
$name = gethostbyaddr($ip);
if( $name == $ip ) continue;
if( array_key_exists($ip, $data) ){
$data[$ip]['newname'] = $name;
} else {
$item = [
'id' => 0,
'oldname' => '',
'newname' => $name,
];
$data[$ip] = $item;
}
}
}
// -----------------------------------------------------------------------------
function update_host($item)
{
global $url, $token, $subnet;
// UPDATE ADDRESS DATA
$data = [
"hostname" => $item['newname']
];
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "addresses/" . $item['id'] . "/");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$headers = [
"token: $token",
"Content-Type: application/json",
'Content-Length: ' . strlen($data_string)
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
if( $server_output == FALSE ){
printf(">>> ERROR: Unable to connect to IPAM: %s\n\n",curl_error($ch));
curl_close ($ch);
exit(1);
}
curl_close ($ch);
$server_json = json_decode($server_output);
if( $server_json->success == false ){
printf(">>> ERROR: Unable to update address %s for %s\n\n",$item['ip'],$subnet);
var_dump($server_json);
exit(1);
}
}
// -----------------------------------------------------------------------------
function new_host($subnetid,$ip,$item)
{
global $url, $token, $subnet;
// NEW ADDRESS DATA
$data = [ "subnetId" => trim($subnetid),
"ip" => $ip,
"hostname" => $item['newname'],
"description" => "only DNS",
"DHCP" => "NO",
];
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "addresses/");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$headers = [
"token: $token",
"Content-Type: application/json",
'Content-Length: ' . strlen($data_string)
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
if( $server_output == FALSE ){
printf(">>> ERROR: Unable to connect to IPAM: %s\n\n",curl_error($ch));
curl_close ($ch);
exit(1);
}
curl_close ($ch);
$server_json = json_decode($server_output);
if( $server_json->success == false ){
printf(">>> ERROR: Unable to create address %s for %s\n\n",$ip,$subnet);
var_dump($server_json);
exit(1);
}
}
// -----------------------------------------------------------------------------
function print_header()
{
// 11 | 147.251.84.1 | ukb-cp.mgmt.ics.muni.cz | ukb-cp.mgmt.ics.muni.cz
printf("\n");
printf("ST Id IP IPAM Name DNS Name \n");
printf("-- ------ ------------------ --------------------------- ---------------------------\n");
}
// -----------------------------------------------------------------------------
function print_data()
{
global $data;
print_header();
foreach($data as $ip => $item){
printf("%2s % 5d | %-16s | %-25s | %-25s\n",'',$item["id"],$ip,$item["oldname"],$item['newname']);
}
}
// -----------------------------------------------------------------------------
function update_data()
{
global $data, $subnetid, $testmode;
print_header();
foreach($data as $ip => $item){
if( ($item["id"] != 0 ) && ($item['newname'] != "") && ($item["oldname"] != $item['newname']) ){
printf("%2s % 5d | %-16s | %-25s | %-25s\n",'M',$item["id"],$ip,$item["oldname"],$item['newname']);
if( $testmode == false ) update_host($item);
}
}
foreach($data as $ip => $item){
if( ($item["id"] == 0 ) && ($item['newname'] != "") ){
printf("%2s % 5d | %-16s | %-25s | %-25s\n",'+',$item["id"],$ip,$item["oldname"],$item['newname']);
if( $testmode == false ) new_host($subnetid,$ip,$item);
}
}
}
// -----------------------------------------------------------------------------
init_session();
get_subnetid();
get_subnetrange();
get_hosts();
scan_dns();
print_data();
update_data();
printf("\n");
?>