Skip to content

Instantly share code, notes, and snippets.

@raegedoc
Forked from spali/10-wancarp
Last active September 15, 2024 16:06
Show Gist options
  • Save raegedoc/093ba815b6b3f2bc2ff327f48c60f3a9 to your computer and use it in GitHub Desktop.
Save raegedoc/093ba815b6b3f2bc2ff327f48c60f3a9 to your computer and use it in GitHub Desktop.
Disable WAN Interface on CARP Backup
#!/usr/local/bin/php
<?php
require_once("config.inc");
require_once("interfaces.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);
}
$ifkey = 'wan';
$lan_vip = 'YOUR_CARP_LAN_VIP';
if ($type === "MASTER") {
log_error("Enable interface '$ifkey' due CARP event '$type'");
$config['interfaces'][$ifkey]['enable'] = '1';
write_config("Enable interface '$ifkey' due CARP event '$type'", false);
interface_configure(false, $ifkey, false, false);
} else if ($type === "BACKUP") {
log_error("Disable interface '$ifkey' due CARP event '$type'");
unset($config['interfaces'][$ifkey]['enable']);
write_config("Disable interface '$ifkey' due CARP event '$type'", false);
interface_configure(false, $ifkey, false, false);
exec('/sbin/route del default >&1', $ifc, $ret);
exec('/sbin/route add default ' . $lan_vip . ' >&1', $ifc, $ret);
}
?>
@raegedoc
Copy link
Author

raegedoc commented Sep 15, 2024

Original script : https://gist.github.com/spali/2da4f23e488219504b2ada12ac59a7dc

I had problems with my backup node not getting internet access back after being promoted primary and then demoted to backup again like explained here : https://gist.github.com/spali/2da4f23e488219504b2ada12ac59a7dc?permalink_comment_id=5189253#gistcomment-5189253

Use this script to setup both your primary and backup nodes :

sudo curl -sL -H "Cache-Control: no-cache" \
https://gist.githubusercontent.com/raegedoc/093ba815b6b3f2bc2ff327f48c60f3a9/raw \
--output /usr/local/etc/rc.syshook.d/carp/10-wancarp && \
sudo chmod +x /usr/local/etc/rc.syshook.d/carp/10-wancarp

Don't forget to adjust YOUR_CARP_LAN_VIP to make it all work.

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