Bläddra i källkod

clear lease for changed record for dnsmasq dhcp server

rajven 3 år sedan
förälder
incheckning
67f04acb81
2 ändrade filer med 24 tillägg och 21 borttagningar
  1. 12 12
      html/admin/customers/control.php
  2. 12 9
      scripts/update-dnsmasq

+ 12 - 12
html/admin/customers/control.php

@@ -5,24 +5,24 @@ require_once ($_SERVER['DOCUMENT_ROOT']."/inc/languages/" . HTML_LANG . ".php");
 
 if (isset($_POST["recheck_ip"]) and is_option($db_link, 37)) {
     $run_cmd = get_option($db_link, 37);
-    shell_exec("sudo $run_cmd >/dev/null 2>/dev/null &");
-    LOG_INFO($db_link, "Run command: $run_cmd");
+    $result = shell_exec("/usr/bin/sudo ".escapeshellcmd($run_cmd)." >/dev/null 2>/dev/null &");
+    LOG_INFO($db_link, "Run command: $run_cmd ");
     header("Location: " . $_SERVER["REQUEST_URI"]);
     exit;
 }
 
 if (isset($_POST["refresh_dhcp"]) and is_option($db_link, 38)) {
     $run_cmd = get_option($db_link, 38);
-    shell_exec("sudo $run_cmd >/dev/null 2>/dev/null &");
-    LOG_INFO($db_link, "Run command: $run_cmd");
+    $result = shell_exec("/usr/bin/sudo ".escapeshellcmd($run_cmd)." >/dev/null 2>/dev/null &");
+    LOG_INFO($db_link, "Run command: $run_cmd ");
     header("Location: " . $_SERVER["REQUEST_URI"]);
     exit;
 }
 
 if (isset($_POST["refresh_nagios"]) and is_option($db_link, 40)) {
     $run_cmd = get_option($db_link, 40);
-    shell_exec("sudo $run_cmd >/dev/null 2>/dev/null &");
-    LOG_INFO($db_link, "Run command: $run_cmd");
+    $result = shell_exec("/usr/bin/sudo ".escapeshellcmd($run_cmd)." >/dev/null 2>/dev/null &");
+    LOG_INFO($db_link, "Run command: $run_cmd ");
     header("Location: " . $_SERVER["REQUEST_URI"]);
     exit;
 }
@@ -36,24 +36,24 @@ if (isset($_POST["up_nagios"])) {
 
 if (isset($_POST["refresh_dns"]) and is_option($db_link, 39)) {
     $run_cmd = get_option($db_link, 39);
-    shell_exec("sudo $run_cmd >/dev/null 2>/dev/null &");
-    LOG_INFO($db_link, "Run command: $run_cmd");
+    $result = shell_exec("/usr/bin/sudo ".escapeshellcmd($run_cmd)." >/dev/null 2>/dev/null &");
+    LOG_INFO($db_link, "Run command: $run_cmd ");
     header("Location: " . $_SERVER["REQUEST_URI"]);
     exit;
 }
 
 if (isset($_POST["discovery"]) and is_option($db_link, 41)) {
     $run_cmd = get_option($db_link, 41);
-    shell_exec("sudo $run_cmd >/dev/null 2>/dev/null &");
-    LOG_DEBUG($db_link, "Run command: $run_cmd");
+    $result = shell_exec("/usr/bin/sudo ".escapeshellcmd($run_cmd)." >/dev/null 2>/dev/null &");
+    LOG_INFO($db_link, "Run command: $run_cmd ");
     header("Location: " . $_SERVER["REQUEST_URI"]);
     exit;
 }
 
 if (isset($_POST["discovery2"]) and is_option($db_link, 41)) {
     $run_cmd = get_option($db_link, 41);
-    shell_exec("sudo $run_cmd 1 >/dev/null 2>/dev/null &");
-    LOG_INFO($db_link, "Run command: $run_cmd 1");
+    $result = shell_exec("/usr/bin/sudo ".escapeshellcmd($run_cmd)." force >/dev/null 2>/dev/null &");
+    LOG_INFO($db_link, "Run command: $run_cmd force");
     header("Location: " . $_SERVER["REQUEST_URI"]);
     exit;
 }

+ 12 - 9
scripts/update-dnsmasq

@@ -13,23 +13,26 @@ if [ ${ret} -ne 0 ]; then
     fi
 
 touch /etc/dnsmasq.d/mac-all
-diff -aqbBfi /tmp/mac-all /etc/dnsmasq.d/mac-all >/dev/null
-ret=$?
+CHANGES=$(diff -ubBi /tmp/mac-all /etc/dnsmasq.d/mac-all | egrep "^[+-]dhcp" | awk '{ print $1 }' | sed -E 's/(\+|\-)//;s/dhcp-host=//;s/\,//' | sort -u)
 
-if [ ${ret} -ne 0 -o -n "${force}" ]; then
+if [ -n "${CHANGES}" -o -n "${force}" ]; then
     logger -t dhcpd "Update dnsmasq config"
     cat /etc/dnsmasq.d/mac-all >/tmp/mac.old
     cat  /tmp/mac-all >/etc/dnsmasq.d/mac-all
     /usr/sbin/dnsmasq --test >/dev/null 2>&1
     ret=$?
     if [ ${ret} -eq 0 ]; then
-        systemctl stop dnsmasq>/dev/null
-        >/var/lib/misc/dnsmasq.leases
-        systemctl start dnsmasq>/dev/null
-	else
+        #clear leases
+        systemctl stop dnsmasq >/dev/null
+        echo "${CHANGES}" | while read LEASE; do
+            echo "Clear ${LEASE}"
+            sed -i "/${LEASE}/d" /var/lib/misc/dnsmasq.leases
+        done
+        systemctl start dnsmasq >/dev/null
+        else
         logger -t dhcpd "Config error! Rollback changes."
-	cat /tmp/mac.old >/etc/dnsmasq.d/mac-all
-	fi
+        cat /tmp/mac.old >/etc/dnsmasq.d/mac-all
+        fi
     else
     logger -t dhcpd "Config not changed. Skip restart"
     fi