-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcpd.leases.php
62 lines (57 loc) · 1.75 KB
/
dhcpd.leases.php
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
<?php
include "config.php";
$contents=file_get_contents("/var/lib/dhcp/dhcpd.leases");
$contents=explode("\n", $contents);
$from = array("binding state", "hardware ethernet", "set vendor-class-identifier =");
$to = array("binding-state", "hardware-ethernet", "vendor-class-identifier");
$current=0;
foreach($contents as $line) {
$line = str_replace($from, $to, $line);
if (strstr($line, "binding") != FALSE)
continue;
switch($current) {
case 0:
if(preg_match("/^\s*(|#.*)$/", $line, $m)) {
}
else if(preg_match("/^lease (.*) {/", $line, $m)) {
$current=$m[1];
}
else if(preg_match("/^server-duid/", $line)) {
// ignore
}
else if(preg_match("/^authoring-byte-order/", $line)) {
// ignore
}
else {
print "Failed parsing '$line'\n";
}
break;
default:
if(preg_match("/^\s*([a-z\-]+) (.*);$/", $line, $m)) {
if (in_array($m[1], array("starts", "ends", "tstp", "cltt")))
$m[2] = substr($m[2], 2);
if (!in_array($m[1], array("uid")))
$data[$current][$m[1]] = trim($m[2], '"');
}
elseif(preg_match("/}/", $line, $m)) {
$current=0;
}
else {
print "Failed parsing '$line'\n";
}
}
}
$db = new PDO('mysql:host=' . $db_host . ';dbname=' . $db_name, $db_user, $db_pass);
$stmt = $db->prepare("truncate leases");
$stmt->execute();
foreach ($data as $ip => $content) {
$query = "INSERT INTO leases (`ip`";
foreach ($content as $key => $val)
$query = $query . ",`" . $key . '`';
$query = $query . ") VALUES ('" . $ip . "'";
foreach ($content as $key => $val)
$query = $query . ",'" . $val . "'";
$query = $query . ")";
$stmt = $db->prepare($query);
$stmt->execute();
}