Project

General

Profile

Download (7.25 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
#!/usr/local/bin/php
2 4d875b4f Scott Ullrich
<?php
3 b46bfcf5 Bill Marquette
/* $Id$ */
4 5b237745 Scott Ullrich
/*
5
	diag_dhcp_leases.php
6 4d875b4f Scott Ullrich
	Copyright (C) 2004 Scott Ullrich
7 5b237745 Scott Ullrich
	All rights reserved.
8 4d875b4f Scott Ullrich
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 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
14
	modification, are permitted provided that the following conditions are met:
15 4d875b4f Scott Ullrich
16 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
17
	   this list of conditions and the following disclaimer.
18 4d875b4f Scott Ullrich
19 5b237745 Scott Ullrich
	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 4d875b4f Scott Ullrich
23 5b237745 Scott Ullrich
	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 b63695db Scott Ullrich
37
$pgtitle = "Diagnostics: DHCP leases";
38
include("head.inc");
39
40 5b237745 Scott Ullrich
?>
41
42
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
43
<?php include("fbegin.inc"); ?>
44 310b2c06 Bill Marquette
<p class="pgtitle"><?=$pgtitle?></p>
45 5b237745 Scott Ullrich
<?php
46
47
flush();
48
49
function leasecmp($a, $b) {
50 5742ca5e Scott Ullrich
        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 5b237745 Scott Ullrich
}
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 69981800 Scott Ullrich
                $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 5b237745 Scott Ullrich
		echo "<tr>\n";
175 5742ca5e Scott Ullrich
                echo "<td class=\"listlr\">{$fspans}{$data['ip']}{$fspane}&nbsp;</td>\n";
176 37c0c49b Scott Ullrich
                if ($data['act'] != "active") {
177
                        echo "<td class=\"listr\">{$fspans}<a href=\"services_wol.php?if={$data['if']}&mac={$data['mac']}\" title=\"send Wake on Lan packet to mac\">{$data['mac']}</a>{$fspane}&nbsp;</td>\n";
178
                } else {
179 5742ca5e Scott Ullrich
                echo "<td class=\"listr\">{$fspans}{$data['mac']}{$fspane}&nbsp;</td>\n";
180 37c0c49b Scott Ullrich
                }
181 5742ca5e Scott Ullrich
                echo "<td class=\"listr\">{$fspans}{$data['hostname']}{$fspane}&nbsp;</td>\n";
182
                echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['start']) . "{$fspane}&nbsp;</td>\n";
183
                echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['end']) . "{$fspane}&nbsp;</td>\n";
184 7477df4f Scott Ullrich
                echo "<td class=\"list\" valign=\"middle\"><a href=\"services_dhcp_edit.php?if={$data['if']}&mac={$data['mac']}&descr={$data['hostname']}\">";
185 4efe9566 Scott Ullrich
		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";
186 7477df4f Scott Ullrich
                echo "<td valign=\"middle\"><a href=\"services_wol_edit.php?if={$data['if']}&mac={$data['mac']}&descr={$data['hostname']}\">";
187 37c0c49b Scott Ullrich
		echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_wol_all.gif\" width=\"17\" height=\"17\" border=\"0\" title=\"add a Wake on Lan mapping for this MAC address\"></a></td>\n";
188 5742ca5e Scott Ullrich
                echo "</tr>\n";
189 5b237745 Scott Ullrich
	}
190
}
191
?>
192
</table>
193
<p>
194
<form action="diag_dhcp_leases.php" method="GET">
195
<input type="hidden" name="order" value="<?=$_GET['order'];?>">
196
<?php if ($_GET['all']): ?>
197
<input type="hidden" name="all" value="0">
198
<input type="submit" class="formbtn" value="Show active leases only">
199
<?php else: ?>
200
<input type="hidden" name="all" value="1">
201
<input type="submit" class="formbtn" value="Show active and expired leases">
202
<?php endif; ?>
203
</form>
204
<?php else: ?>
205
<p><strong>No leases file found. Is the DHCP server active?</strong></p>
206
<?php endif; ?>
207 fda1fdae Scott Ullrich
208
<meta http-equiv="refresh" content="120;url=<?php print $_SERVER['PHP_SELF']; ?>">
209
210 5b237745 Scott Ullrich
<?php include("fend.inc"); ?>
211
</body>
212
</html>