Add and remove static DHCPv4 releases using Settings page, prevent adding more than one entry with the same MAC address

This commit is contained in:
DL6ER
2017-01-25 11:44:17 +01:00
parent 1eb8f32755
commit 915bbb3e5d
2 changed files with 87 additions and 5 deletions
+77 -1
View File
@@ -38,6 +38,34 @@ function validMAC($mac_addr)
return (preg_match('/([a-fA-F0-9]{2}[:]?){6}/', $mac_addr) == 1);
}
$dhcp_static_leases = array();
function readStaticLeasesFile()
{
global $dhcp_static_leases;
$dhcp_static_leases = array();
$dhcpstatic = @fopen('/etc/dnsmasq.d/04-pihole-static-dhcp.conf', 'r');
if(!is_resource($dhcpstatic))
return false;
while(!feof($dhcpstatic))
{
// Remove any possibly existing variable with this name
$mac = "";
sscanf(trim(fgets($dhcpstatic)),"dhcp-host=%[^,],%[^,],%[^,]",$mac,$one,$two);
if(strlen($mac) > 0)
{
if(validIP($one) && strlen($two) == 0)
array_push($dhcp_static_leases,["hwaddr"=>$mac, "IP"=>$one, "host"=>""]);
elseif(strlen($two) == 0)
array_push($dhcp_static_leases,["hwaddr"=>$mac, "IP"=>"", "host"=>$one]);
else
array_push($dhcp_static_leases,["hwaddr"=>$mac, "IP"=>$one, "host"=>$two]);
}
}
return true;
}
$DNSserverslist = [
"8.8.8.8" => "Google (Primary)",
"208.67.222.222" => "OpenDNS (Primary)",
@@ -328,19 +356,67 @@ function validMAC($mac_addr)
$mac = $_POST["AddMAC"];
$ip = $_POST["AddIP"];
$hostname = $_POST["AddHostname"];
if(!validMAC($mac))
{
$error .= "MAC address (".htmlentities($mac).") is invalid!<br>";
}
$mac = strtoupper($mac);
if(!validIP($ip))
if(!validIP($ip) && strlen($ip) > 0)
{
$error .= "IP address (".htmlentities($ip).") is invalid!<br>";
}
if(!validDomain($hostname) && strlen($hostname) > 0)
{
$error .= "Host name (".htmlentities($hostname).") is invalid!<br>";
}
if(strlen($hostname) == 0 && strlen($ip) == 0)
{
$error .= "You can not omit both the IP address and the host name!<br>";
}
if(strlen($hostname) == 0)
$hostname = "nohost";
if(strlen($ip) == 0)
$ip = "noip";
// Test if this MAC address is already included
readStaticLeasesFile();
foreach($dhcp_static_leases as $lease) {
if($lease["hwaddr"] === $mac)
{
$error .= "Static release for MAC address (".htmlentities($mac).") already defined!<br>";
break;
}
}
if(!strlen($error))
{
exec("sudo pihole -a addstaticdhcp ".$mac." ".$ip." ".$hostname);
$success .= "A new static address has been added";
}
break;
}
if(isset($_POST["removestatic"]))
{
$debug = true;
$mac = $_POST["removestatic"];
if(!validMAC($mac))
{
$error .= "MAC address (".htmlentities($mac).") is invalid!<br>";
}
$mac = strtoupper($mac);
if(!strlen($error))
{
exec("sudo pihole -a removestaticdhcp ".$mac);
$success .= "The static address with MAC address ".htmlentities($mac)." has been added";
}
break;
}
+10 -4
View File
@@ -257,8 +257,10 @@
// Read leases file
$leasesfile = true;
$dhcpleases = fopen('/etc/pihole/dhcp.leases', 'r') or $leasesfile = false;
$dhcp_leases = [];
$dhcpleases = @fopen('/etc/pihole/dhcp.leases', 'r');
if(!is_resource($dhcpleases ))
$leasesfile = false;
$dhcp_leases = array();
function convertseconds($argument) {
$seconds = round($argument);
@@ -325,9 +327,12 @@
array_push($dhcp_leases,["TIME"=>$time, "hwaddr"=>$line[1], "IP"=>$line[2], "host"=>$host, "clid"=>$clid, "type"=>$type]);
}
}
readStaticLeasesFile();
?>
<div class="col-md-12">
<div class="box box-warning collapsed-box">
<div class="box box-warning <?php if(!isset($_POST["addstatic"])){ ?>collapsed-box<?php } ?>">
<div class="box-header with-border">
<h3 class="box-title">DHCP leases</h3>
<div class="box-tools pull-right"><button type="button" class="btn btn-box-tool" data-widget="collapse" id="leaseexpand"><i class="fa fa-plus"></i></button></div>
@@ -359,10 +364,11 @@
</tr>
</thead>
<tbody>
<?php foreach($static_dhcp_leases as $lease) { ?><tr><td><?php echo $lease["hwaddr"]; ?></td><td><?php echo $lease["IP"]; ?></td><td><?php echo $lease["host"]; ?></td><td><button class="btn btn-danger btn-xs" type="button"><span class="glyphicon glyphicon-trash"></span></button></td></tr><?php } ?>
<?php foreach($dhcp_static_leases as $lease) { ?><tr><td><?php echo $lease["hwaddr"]; ?></td><td><?php echo $lease["IP"]; ?></td><td><?php echo $lease["host"]; ?></td><td><button class="btn btn-danger btn-xs" type="submit" name="removestatic" value="<?php echo $lease["hwaddr"]; ?>"><span class="glyphicon glyphicon-trash"></span></button></td></tr><?php } ?>
<tr><td><input type="text" name="AddMAC"></td><td><input type="text" name="AddIP"></td><td><input type="text" name="AddHostname"></td><td><button class="btn btn-success btn-xs" type="submit" name="addstatic"><span class="glyphicon glyphicon-plus"></span></button></td></tr>
</tbody>
</table>
<p>Specifying the MAC address is mandatory. If the IP address is omitted, then it will be generated dynamically. Hoever, the specified host name is used instead of the host name the machine claims to be using. If the host name is omitted, only a static release will be added.</p>
</div>
</div>
</div>