. */ // ************************************************************************************************************** // // The purpose of this script is to dynamically remove hosts from your local network. Well, hosts won't actually // be kicked off the network, but they will be moved into the unknown hosts pool. See add_host.php for more info. // Hosts can be removed individually, or more interesting, hosts can be removed after their expiration date if // you have added hosts using the add_host.php script and set an expiration date. To do this automatically, maybe // add a script to your hourly cron like the following: // // #!/bin/sh // wget -O - http://username:password@localhost/secure/rm_host.php?old=true > /dev/null 2>&1 // // And boom! You can temporarily add hosts to the known pool of your local network and have them auto-expire // // ************************************************************************************************************** // ************************************************************************************************************** // // Apache will need to be able to sudo your dhcpd init script, and write to dhcpd.conf and the hosts file: // visudo: www-data ALL=NOPASSWD: /etc/init.d/dhcp3-server // // ************************************************************************************************************** $dhcpd_conf_path = "/etc/dhcp3/dhcpd.conf"; $dhcpd_init_path = "/etc/init.d/dhcp3-server"; $hosts_path = "/etc/hosts"; $admin = str_replace("!", "@", "root!jusme.org"); print "\n"; print "\n"; print "Remove Host\n"; print "\n"; print "\n"; print "\n"; if ($_GET["old"] == "true") { $removed = array(); $hosts = $orig_hosts = file_get_contents($hosts_path); $dhcpd = $orig_dhcpd = file_get_contents($dhcpd_conf_path); // Match time-limited hostnames $num = preg_match_all("/ (.+)_(\d+)_(\d+)_(\d+)_(\d+)_(\d+)/", $hosts, $matches); for ($i = 0; $i < $num; $i++) { $name = $matches[1][$i]; // Convert back to proper timestamp format $time = strtotime($matches[2][$i] . "/" . $matches[3][$i] . "/" . $matches[4][$i] . " " . $matches[5][$i] . ":" . $matches[6][$i]); // If the current time is past the expiration date for this hostname if (time() >= $time) { // Remove hostname $hosts = preg_replace("/\d+\.\d+\.\d+\.\d+\t$name( .*)?\\n/U", "", $hosts); $dhcpd = preg_replace("/host $name\\n{.*}\\n/sU", "", $dhcpd); $removed[] = $name; } } file_put_contents($hosts_path, $hosts); file_put_contents($dhcpd_conf_path, $dhcpd); passthru("sudo $dhcpd_init_path restart", $ret); // Restarting dhcpd failed, probably malformed conf file if ($ret != 0) { // Revert back to previous data, which presumably was working before file_put_contents($hosts_path, $orig_hosts); file_put_contents($dhcpd_conf_path, $orig_dhcpd); passthru("sudo $dhcpd_init_path restart", $ret); // Send a warning email to the administrator system("echo 'Remove old hosts failed!' | mail -s 'DHCPD' $admin"); die('DHCPD restart failed!'); } if (!empty($removed)) { print "

Old hosts removed:\n"; foreach ($removed as $name) print "
$name\n"; } } elseif ($_GET["submit"] == "true") { $name = $_POST["name"]; $dhcpd = $orig_dhcpd = file_get_contents($dhcpd_conf_path); $dhcpd = preg_replace("/host $name\\n{.*}\\n/sU", "", $dhcpd); file_put_contents($dhcpd_conf_path, $dhcpd); passthru("sudo $dhcpd_init_path restart", $ret); // Restarting dhcpd failed, probably malformed conf file if ($ret != 0) { // Revert back to previous data, which presumably was working before file_put_contents($dhcpd_conf_path, $orig_dhcpd); passthru("sudo $dhcpd_init_path restart", $ret); die('DHCPD restart failed!'); } $hosts = file_get_contents($hosts_path); $hosts = preg_replace("/\d+\.\d+\.\d+\.\d+\t$name( .*)?\\n/U", "", $hosts); file_put_contents($hosts_path, $hosts); print "

Host removed.\n"; } else { $dhcpd = file_get_contents($dhcpd_conf_path); preg_match_all("/host (.*)\\n{/U", $dhcpd, $matches); print "

\n"; print "Remove Host: \n"; print "

\n"; print "

\n"; print "

OR

\n"; print "\n"; } print "\n"; print "\n"; ?>