Project

General

Profile

Download (5.82 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/*
4
	diag_dhcp_leases.php
5
	Copyright (C) 2004 Scott Ullrich
6
	All rights reserved.
7

    
8
	originially part of m0n0wall (http://m0n0.ch/wall)
9
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
	All rights reserved.
11

    
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14

    
15
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17

    
18
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21

    
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33

    
34
require("guiconfig.inc");
35
?>
36
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
37
<html>
38
<head>
39
<title><?=gentitle("Diagnostics: DHCP leases");?></title>
40
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
41
<link href="gui.css" rel="stylesheet" type="text/css">
42
</head>
43

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

    
49
flush();
50

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

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

    
57
if ($fp):
58

    
59
$return = array();
60

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

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

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

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

    
93
	} while($line);
94

    
95
	$lines++;
96
}
97

    
98
fclose($fp);
99

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

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

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