Skip to content

Instantly share code, notes, and snippets.

@edward-scroop
Last active October 13, 2025 07:57
Show Gist options
  • Select an option

  • Save edward-scroop/c717f6368fd3911be6790054bf6c70d2 to your computer and use it in GitHub Desktop.

Select an option

Save edward-scroop/c717f6368fd3911be6790054bf6c70d2 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/php
<?php
require_once("config.inc");
require_once("system.inc");
require_once("interfaces.inc");
require_once("interfaces.lib.inc");
require_once("util.inc");
$subsystem = !empty($argv[1]) ? $argv[1] : '';
$type = !empty($argv[2]) ? $argv[2] : '';
if ($type != 'MASTER' && $type != 'BACKUP' && $type != 'INIT') {
log_error("Carp '$type' event unknown from source '{$subsystem}'");
exit(1);
}
if (!strstr($subsystem, '@')) {
log_error("Carp '$type' event triggered from wrong source '{$subsystem}'");
exit(1);
}
// Name of wan interface you wish to enable/disable
$wan_int = 'wan';
$ifkey = $config['interfaces'][$wan_int]['if'];
if ($type === 'MASTER') {
if ($config['interfaces'][$wan_int]['enable'] == 0) {
log_msg("enabled interface '$wan_int($ifkey)' due CARP event '$type'");
$config['interfaces'][$wan_int]['enable'] = '1';
interface_configure(false, $wan_int, true, true);
legacy_interface_flags($ifkey, 'up');
write_config("enabled interface '$wan_int($ifkey)' due CARP event '$type'", false);
} else {
log_msg("Carp '$type' duplicate event triggered.");
}
} else if ($type === 'BACKUP') {
if ($config['interfaces'][$wan_int]['enable'] == 1) {
log_msg("disabled interface '$wan_int($ifkey)' due CARP event '$type'");
interface_reset($wan_int);
unset($config['interfaces'][$wan_int]['enable']);
interface_configure(false, $wan_int, true, false);
legacy_interface_flags($ifkey, 'down');
write_config("disable interface '$wan_int($ifkey)' due CARP event '$type'", false);
} else {
log_msg("Carp '$type' duplicate event triggered.");
}
}
?>
curl -sL -H "Cache-Control: no-cache" \
https://gist.githubusercontent.com/edward-scroop/c717f6368fd3911be6790054bf6c70d2/raw/10-wancarp \
--output /usr/local/etc/rc.syshook.d/carp/10-wancarp && \
chmod +x /usr/local/etc/rc.syshook.d/carp/10-wancarp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment