Project

General

Profile

Download (5.83 KB) Statistics
| Branch: | Tag: | Revision:
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
38
<html>
39
<head>
40
<title><?=gentitle("Diagnostics: DHCP leases");?></title>
41
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
42
<link href="gui.css" rel="stylesheet" type="text/css">
43
</head>
44

    
45
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
46
<?php include("fbegin.inc"); ?>
47
<p class="pgtitle">Diagnostics: DHCP leases</p>
48
<?php
49

    
50
flush();
51

    
52
function leasecmp($a, $b) {
53
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
54
}
55

    
56
$fp = @fopen("{$g['vardb_path']}/dhcpd.leases","r");
57

    
58
if ($fp):
59

    
60
$return = array();
61

    
62
while ($line = fgets($fp)) {
63
	$matches = "";
64

    
65
	// Sort out comments
66
	// C-style comments not supported!
67
	if (preg_match("/^\s*[\r|\n]/", $line, $matches[0]) ||
68
				preg_match("/^([^\"#]*)#.*$/", $line, $matches[1]) ||
69
				preg_match("/^([^\"]*)\/\/.*$/", $line, $matches[2]) ||
70
				preg_match("/\s*#(.*)/", $line, $matches[3]) ||
71
				preg_match("/\\\"\176/", $line, $matches[4])
72
		) {
73
		$line = "";
74
		continue;
75
	}
76

    
77
	if (preg_match("/(.*)#(.*)/", $line, $matches))
78
		$line = $matches[0];
79

    
80
	// Tokenize lines
81
	do {
82
		if (preg_match("/^\s*\"([^\"]*)\"(.*)$/", $line, $matches)) {
83
			$line = $matches[2];
84
			$return[] = array($matches[1], 0);
85
		} else if (preg_match("/^\s*([{};])(.*)$/", $line, $matches)) {
86
			$line = $matches[2];
87
			$return[] = array($matches[0], 1);
88
		} else if (preg_match("/^\s*([^{}; \t]+)(.*)$/", $line, $matches)) {
89
			$line = $matches[2];
90
			$return[] = array($matches[1], 0);
91
		} else
92
			break;
93

    
94
	} while($line);
95

    
96
	$lines++;
97
}
98

    
99
fclose($fp);
100

    
101
$leases = array();
102
$i = 0;
103

    
104
// Put everything together again
105
while ($data = array_shift($return)) {
106
	if ($data[0] == "next") {
107
		$d = array_shift($return);
108
	}
109
	if ($data[0] == "lease") {
110
		$d = array_shift($return);
111
		$leases[$i]['ip'] = $d[0];
112
	}
113
	if ($data[0] == "client-hostname") {
114
		$d = array_shift($return);
115
		$leases[$i]['hostname'] = $d[0];
116
	}
117
	if ($data[0] == "hardware") {
118
		$d = array_shift($return);
119
		if ($d[0] == "ethernet") {
120
			$d = array_shift($return);
121
			$leases[$i]['mac'] = $d[0];
122
		}
123
	} else if ($data[0] == "starts") {
124
		$d = array_shift($return);
125
		$d = array_shift($return);
126
		$leases[$i]['start'] = $d[0];
127
		$d = array_shift($return);
128
		$leases[$i]['start'] .= " " . $d[0];
129
	} else if ($data[0] == "ends") {
130
		$d = array_shift($return);
131
		$d = array_shift($return);
132
		$leases[$i]['end'] = $d[0];
133
		$d = array_shift($return);
134
		$leases[$i]['end'] .= " " . $d[0];
135
	} else if ($data[0] == "binding") {
136
		$d = array_shift($return);
137
		if ($d[0] == "state") {
138
			$d = array_shift($return);
139
			$leases[$i]['act'] = $d[0];
140
		}
141
	} else if (($data[0] == "}") && ($data[1] == 1))		// End of group
142
		$i++;
143
}
144

    
145
if ($_GET['order'])
146
	usort($leases, "leasecmp");
147
?>
148
<table width="100%" border="0" cellpadding="0" cellspacing="0">
149
  <tr>
150
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=ip">IP address</a></td>
151
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=mac">MAC address</a></td>
152
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=hostname">Hostname</a></td>
153
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=start">Start</a></td>
154
    <td class="listhdr"><a href="?all=<?=$_GET['all'];?>&order=end">End</a></td>
155
	</tr>
156
<?php
157
foreach ($leases as $data) {
158
	if (($data['act'] == "active") || ($_GET['all'] == 1)) {
159
		if ($data['act'] != "active") {
160
			$fspans = "<span class=\"gray\">";
161
			$fspane = "</span>";
162
		} else {
163
			$fspans = $fspane = "";
164
		}
165
		echo "<tr>\n";
166
		echo "<td class=\"listlr\">{$fspans}{$data['ip']}{$fspane}&nbsp;</td>\n";
167
		echo "<td class=\"listr\">{$fspans}{$data['mac']}{$fspane}&nbsp;</td>\n";
168
		echo "<td class=\"listr\">{$fspans}{$data['hostname']}{$fspane}&nbsp;</td>\n";
169
		echo "<td class=\"listr\">{$fspans}{$data['start']}{$fspane}&nbsp;</td>\n";
170
		echo "<td class=\"listr\">{$fspans}{$data['end']}{$fspane}&nbsp;</td>\n";
171
		echo "</tr>\n";
172
	}
173
}
174
?>
175
</table>
176
<p>
177
<form action="diag_dhcp_leases.php" method="GET">
178
<input type="hidden" name="order" value="<?=$_GET['order'];?>">
179
<?php if ($_GET['all']): ?>
180
<input type="hidden" name="all" value="0">
181
<input type="submit" class="formbtn" value="Show active leases only">
182
<?php else: ?>
183
<input type="hidden" name="all" value="1">
184
<input type="submit" class="formbtn" value="Show active and expired leases">
185
<?php endif; ?>
186
</form>
187
<?php else: ?>
188
<p><strong>No leases file found. Is the DHCP server active?</strong></p>
189
<?php endif; ?>
190
<?php include("fend.inc"); ?>
191
</body>
192
</html>
(6-6/117)