#!/usr/bin/php getConfigs(); $config = new Config(); $config->read(); $routeParser = new RoutingTableReader(); $routes = $routeParser->getRoutes(); $defGatewayInterface = ""; $defGateway = ""; foreach ($routes as $route) { if ($route["dst"] === "0.0.0.0/0") { $defGatewayInterface = $route["dev"]; $defGateway = $route["gateway"]; break; } } if (!$defGatewayInterface) { throw new RuntimeException("Failed to detect default gateway interface"); } $contents = file_get_contents(CFGFILE); $lines = explode("\n", $contents); //remove existing routes foreach ($lines as $key => $line) { if (str_starts_with($line, REM_PREFIX) or str_starts_with($line, "ip route ") and str_contains($line . " ", $defGateway . " ")) { unset($lines[$key]); } } //add new routes foreach ($config["networks"] as $key) { $lines[] = REM_PREFIX . $key; if (isset($networks[$key])) { foreach ($networks[$key]["networks"] as $route) { $lines[] = "ip route " . $route . " " . $defGateway; } } } foreach ($lines as $key => $line) { if (trim($line) === "") { unset($lines[$key]); } } $backupFile = CFGFILE . ".sav"; unlink($backupFile); rename(CFGFILE, $backupFile); file_put_contents(CFGFILE, implode("\n", $lines)); //restart zebra echo shell_exec($config["zebra_restart_cmd"]); } catch (Exception $e) { echo "\nError:" . $e->getMessage() . "\n"; exit(1); } exit(0);