1
|
#!/usr/local/bin/php
|
2
|
<?php
|
3
|
/* $Id$ */
|
4
|
/*
|
5
|
diag_dhcp_leases.php
|
6
|
Copyright (C) 2004 Scott Ullrich
|
7
|
All rights reserved.
|
8
|
|
9
|
originially part of m0n0wall (http://m0n0.ch/wall)
|
10
|
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
11
|
All rights reserved.
|
12
|
|
13
|
Redistribution and use in source and binary forms, with or without
|
14
|
modification, are permitted provided that the following conditions are met:
|
15
|
|
16
|
1. Redistributions of source code must retain the above copyright notice,
|
17
|
this list of conditions and the following disclaimer.
|
18
|
|
19
|
2. Redistributions in binary form must reproduce the above copyright
|
20
|
notice, this list of conditions and the following disclaimer in the
|
21
|
documentation and/or other materials provided with the distribution.
|
22
|
|
23
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
24
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
25
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
26
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
27
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
POSSIBILITY OF SUCH DAMAGE.
|
33
|
*/
|
34
|
|
35
|
require("guiconfig.inc");
|
36
|
|
37
|
$pgtitle = "Diagnostics: DHCP leases";
|
38
|
include("head.inc");
|
39
|
|
40
|
?>
|
41
|
|
42
|
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
|
43
|
<?php include("fbegin.inc"); ?>
|
44
|
<p class="pgtitle"><?=$pgtitle?></p>
|
45
|
<?php
|
46
|
|
47
|
flush();
|
48
|
|
49
|
function leasecmp($a, $b) {
|
50
|
return strcmp($a[$_GET['order']], $b[$_GET['order']]);
|
51
|
}
|
52
|
|
53
|
function adjust_gmt($dt) {
|
54
|
$ts = strtotime($dt . " GMT");
|
55
|
return strftime("%Y/%m/%d %H:%M:%S", $ts);
|
56
|
}
|
57
|
|
58
|
$fp = @fopen("{$g['vardb_path']}/dhcpd.leases","r");
|
59
|
|
60
|
if ($fp):
|
61
|
|
62
|
$return = array();
|
63
|
|
64
|
while ($line = fgets($fp)) {
|
65
|
$matches = "";
|
66
|
|
67
|
// Sort out comments
|
68
|
// C-style comments not supported!
|
69
|
if (preg_match("/^\s*[\r|\n]/", $line, $matches[0]) ||
|
70
|
preg_match("/^([^\"#]*)#.*$/", $line, $matches[1]) ||
|
71
|
preg_match("/^([^\"]*)\/\/.*$/", $line, $matches[2]) ||
|
72
|
preg_match("/\s*#(.*)/", $line, $matches[3]) ||
|
73
|
preg_match("/\\\"\176/", $line, $matches[4])
|
74
|
) {
|
75
|
$line = "";
|
76
|
continue;
|
77
|
}
|
78
|
|
79
|
if (preg_match("/(.*)#(.*)/", $line, $matches))
|
80
|
$line = $matches[0];
|
81
|
|
82
|
// Tokenize lines
|
83
|
do {
|
84
|
if (preg_match("/^\s*\"([^\"]*)\"(.*)$/", $line, $matches)) {
|
85
|
$line = $matches[2];
|
86
|
$return[] = array($matches[1], 0);
|
87
|
} else if (preg_match("/^\s*([{};])(.*)$/", $line, $matches)) {
|
88
|
$line = $matches[2];
|
89
|
$return[] = array($matches[0], 1);
|
90
|
} else if (preg_match("/^\s*([^{}; \t]+)(.*)$/", $line, $matches)) {
|
91
|
$line = $matches[2];
|
92
|
$return[] = array($matches[1], 0);
|
93
|
} else
|
94
|
break;
|
95
|
|
96
|
} while($line);
|
97
|
|
98
|
$lines++;
|
99
|
}
|
100
|
|
101
|
fclose($fp);
|
102
|
|
103
|
$leases = array();
|
104
|
$i = 0;
|
105
|
|
106
|
// Put everything together again
|
107
|
while ($data = array_shift($return)) {
|
108
|
if ($data[0] == "next") {
|
109
|
$d = array_shift($return);
|
110
|
}
|
111
|
if ($data[0] == "lease") {
|
112
|
$d = array_shift($return);
|
113
|
$leases[$i]['ip'] = $d[0];
|
114
|
}
|
115
|
if ($data[0] == "client-hostname") {
|
116
|
$d = array_shift($return);
|
117
|
$leases[$i]['hostname'] = $d[0];
|
118
|
}
|
119
|
if ($data[0] == "hardware") {
|
120
|
$d = array_shift($return);
|
121
|
if ($d[0] == "ethernet") {
|
122
|
$d = array_shift($return);
|
123
|
$leases[$i]['mac'] = $d[0];
|
124
|
}
|
125
|
} else if ($data[0] == "starts") {
|
126
|
$d = array_shift($return);
|
127
|
$d = array_shift($return);
|
128
|
$leases[$i]['start'] = $d[0];
|
129
|
$d = array_shift($return);
|
130
|
$leases[$i]['start'] .= " " . $d[0];
|
131
|
} else if ($data[0] == "ends") {
|
132
|
$d = array_shift($return);
|
133
|
$d = array_shift($return);
|
134
|
$leases[$i]['end'] = $d[0];
|
135
|
$d = array_shift($return);
|
136
|
$leases[$i]['end'] .= " " . $d[0];
|
137
|
} else if ($data[0] == "binding") {
|
138
|
$d = array_shift($return);
|
139
|
if ($d[0] == "state") {
|
140
|
$d = array_shift($return);
|
141
|
$leases[$i]['act'] = $d[0];
|
142
|
}
|
143
|
} else if (($data[0] == "}") && ($data[1] == 1)) // End of group
|
144
|
$i++;
|
145
|
}
|
146
|
|
147
|
if ($_GET['order'])
|
148
|
usort($leases, "leasecmp");
|
149
|
?>
|
150
|
<table width="100%" border="0" cellpadding="0" cellspacing="0">
|
151
|
<tr>
|
152
|
<td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=ip">IP address</a></td>
|
153
|
<td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=mac">MAC address</a></td>
|
154
|
<td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=hostname">Hostname</a></td>
|
155
|
<td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=start">Start</a></td>
|
156
|
<td class="listhdr"><a href="?all=<?=$_GET['all'];?>&order=end">End</a></td>
|
157
|
</tr>
|
158
|
<?php
|
159
|
foreach ($leases as $data) {
|
160
|
if (($data['act'] == "active") || ($_GET['all'] == 1)) {
|
161
|
if ($data['act'] != "active") {
|
162
|
$fspans = "<span class=\"gray\">";
|
163
|
$fspane = "</span>";
|
164
|
} else {
|
165
|
$fspans = $fspane = "";
|
166
|
}
|
167
|
$lip = ip2long($data['ip']);
|
168
|
foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
|
169
|
if (($lip >= ip2long($dhcpifconf['range']['from'])) && ($lip <= ip2long($dhcpifconf['range']['to']))) {
|
170
|
$data['if'] = $dhcpif;
|
171
|
break;
|
172
|
}
|
173
|
}
|
174
|
echo "<tr>\n";
|
175
|
echo "<td class=\"listlr\">{$fspans}{$data['ip']}{$fspane} </td>\n";
|
176
|
echo "<td class=\"listr\">{$fspans}{$data['mac']}{$fspane} </td>\n";
|
177
|
echo "<td class=\"listr\">{$fspans}{$data['hostname']}{$fspane} </td>\n";
|
178
|
echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['start']) . "{$fspane} </td>\n";
|
179
|
echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['end']) . "{$fspane} </td>\n";
|
180
|
echo "<td class=\"list\" valign=\"middle\"><a href=\"services_dhcp_edit.php?if={$data['if']}&mac={$data['mac']}\">";
|
181
|
echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_plus.gif\" width=\"17\" height=\"17\" border=\"0\" title=\"add a static mapping for this MAC address\"></a></td>\n";
|
182
|
echo "</tr>\n";
|
183
|
}
|
184
|
}
|
185
|
?>
|
186
|
</table>
|
187
|
<p>
|
188
|
<form action="diag_dhcp_leases.php" method="GET">
|
189
|
<input type="hidden" name="order" value="<?=$_GET['order'];?>">
|
190
|
<?php if ($_GET['all']): ?>
|
191
|
<input type="hidden" name="all" value="0">
|
192
|
<input type="submit" class="formbtn" value="Show active leases only">
|
193
|
<?php else: ?>
|
194
|
<input type="hidden" name="all" value="1">
|
195
|
<input type="submit" class="formbtn" value="Show active and expired leases">
|
196
|
<?php endif; ?>
|
197
|
</form>
|
198
|
<?php else: ?>
|
199
|
<p><strong>No leases file found. Is the DHCP server active?</strong></p>
|
200
|
<?php endif; ?>
|
201
|
<?php include("fend.inc"); ?>
|
202
|
</body>
|
203
|
</html>
|