Project

General

Profile

Download (5.8 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php 
3
/*
4
	diag_dhcp_leases.php
5
	part of m0n0wall (http://m0n0.ch/wall)
6
	
7
	Copyright (C) 2003-2004 Bj?rn P?lsson <bjorn@networksab.com> and Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9
	
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12
	
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15
	
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19
	
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

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

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

    
47
flush();
48

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

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

    
55
if ($fp):
56

    
57
$return = array();
58

    
59
while ($line = fgets($fp)) {
60
	$matches = "";
61

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

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

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

    
91
	} while($line);
92

    
93
	$lines++;
94
}
95

    
96
fclose($fp);
97

    
98
$leases = array();
99
$i = 0;
100

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

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