Skip to content

Instantly share code, notes, and snippets.

@luckman212
Last active July 18, 2023 22:58
Show Gist options
  • Save luckman212/0fdea1cbdc0a561d781a52c7d34fb60d to your computer and use it in GitHub Desktop.
Save luckman212/0fdea1cbdc0a561d781a52c7d34fb60d to your computer and use it in GitHub Desktop.
dpinger_static_routes.php - helper script related to https://github.com/pfsense/pfsense/pull/4591
<?php
// includes
require_once("functions.inc");
require_once("system.inc");
require_once("util.inc");
// global dpinger_dont_add_static_routes option
$dp_global_sr_disable = (isset($config['system']['dpinger_dont_add_static_routes']) ? 'ENABLED (checked)' : 'default (unchecked)');
printf("==> global `dpinger_dont_add_static_routes` setting: %s\n", $dp_global_sr_disable);
// list each gateway and its monitor IP
if (is_array($config['gateways'])) {
printf("\n%-16s %-8s %-20s %-20s %s\n", "GATEWAYS", "IF", "GW IP", "MON IP", "STATIC ROUTE");
echo "===================================================================================\n";
$gateways = $config['gateways']['gateway_item'];
foreach ($gateways as $i => $gw) {
$if = (isset($gw['interface']) ? $gw['interface'] : '?');
$gwip = (isset($gw['gateway']) ? $gw['gateway'] : '?');
$name = (isset($gw['name']) ? $gw['name'] : '?');
$sr = (isset($gw['dpinger_dont_add_static_route']) ? 'NO (checked)' : 'yes (unchecked)');
$monip = (isset($gw['monitor']) ? $gw['monitor'] : '(blank)');
printf("%-16s %-8s %-20s %-20s %s\n", $name, $if, $gwip, $monip, $sr);
}
} else {
echo "(no gateways found)\n";
}
// print any static routes
$sroutes = get_staticroutes();
if (is_array($sroutes)) {
printf("\n%-24s %-24s %s\n", "STATIC ROUTES", "GATEWAY", "DESC");
echo "===================================================================================\n";
foreach ($sroutes as $sr) {
printf("%-24s %-24s %s\n", $sr['network'], $sr['gateway'], $sr['descr']);
}
}
/* print routing table (IPv4)
$routes = shell_exec('/usr/bin/netstat -rnl -f inet');
if (!empty($routes)) {
echo $routes;
}
*/
$rt = route_table();
if (is_array($rt)) {
printf("\nSTATIC ROUTES FROM ROUTING TABLE\n");
echo "===================================================================================\n";
$rt4 = $rt['inet'];
$rt6 = $rt['inet6'];
foreach ($rt4 as $r) {
if ($r['flags'] == 'UGHS') {
printf("%-24s %-30s %-6s %s\n", $r['destination'], $r['gateway'], $r['flags'], $r['interface-name']);
}
}
foreach ($rt6 as $r) {
if ($r['flags'] == 'UGHS') {
printf("%-24s %-30s %-6s %s\n", $r['destination'], $r['gateway'], $r['flags'], $r['interface-name']);
}
}
}
?>
@luckman212
Copy link
Author

luckman212 commented Jun 2, 2022

To run:

  1. install cmdwatch:
pkg add https://pkg.freebsd.org/FreeBSD:12:amd64/latest/All/cmdwatch-0.2.0_2.txz
  1. download the script:
fetch https://gist.githubusercontent.com/luckman212/0fdea1cbdc0a561d781a52c7d34fb60d/raw/ffd321ef196fb1c919dd66700acdd4acc02b3e63/dpinger_static_routes.php
  1. then
cmdwatch --interval=2 'php -q dpinger_static_routes.php'

or, if you don't want to install cmdwatch, you can use a simple while loop

while : ; do clear; php -q dpinger_static_routes.php ; sleep 2 ; done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment