changed name and made the backup script only update when carp is in backup state
This commit is contained in:
78
opnsense-carp-dhcp-backup-gateway.php
Normal file
78
opnsense-carp-dhcp-backup-gateway.php
Normal file
@@ -0,0 +1,78 @@
|
||||
#!/usr/local/bin/php
|
||||
<?php
|
||||
|
||||
require_once("config.inc");
|
||||
require_once("interfaces.inc");
|
||||
require_once("util.inc");
|
||||
require_once("filter.inc");
|
||||
require_once("util.inc");
|
||||
require_once("system.inc");
|
||||
require_once('rrd.inc');
|
||||
require_once('plugins.inc.d/webgui.inc');
|
||||
|
||||
#############
|
||||
# Variables #
|
||||
#############
|
||||
|
||||
$wanInterface = 'wan';
|
||||
$gatewayName = 'WAN_GW';
|
||||
|
||||
###################
|
||||
# Start of Script #
|
||||
###################
|
||||
|
||||
function write_config_and_restart_services ($interface) {
|
||||
write_config();
|
||||
system_hosts_generate(true);
|
||||
system_resolvconf_generate(true);
|
||||
interface_bring_down($interface);
|
||||
interface_configure(true, $interface, true);
|
||||
plugins_configure('monitor', true);
|
||||
filter_configure_sync(true);
|
||||
webgui_configure_do(true);
|
||||
rrd_configure(true);
|
||||
}
|
||||
|
||||
$vip = $config['virtualip']['vip'];
|
||||
$intf_details = legacy_interfaces_details();
|
||||
$intf = get_real_interface($wanInterface);
|
||||
|
||||
foreach ($vip as $carp) {
|
||||
if ($carp['interface'] == $wanInterface) {
|
||||
$wanCarp = $carp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
# Carp Status MASTER or BACKUP
|
||||
|
||||
$intf_details = legacy_interfaces_details();
|
||||
$intf = get_real_interface($wanInterface);
|
||||
# loop through carp addresses and update the wan ip address
|
||||
foreach ($config['virtualip']['vip'] as $i => $vip) {
|
||||
if ($vip['interface'] == $wanInterface) {
|
||||
$carpStatus = $intf_details[$intf]['carp'][$vip['vhid']]['status'];
|
||||
if ($carpStatus == "BACKUP") {
|
||||
$description = $config['virtualip']['vip'][$i]['descr'];
|
||||
printf("Description from VIP WAN: %s \n", $description);
|
||||
# VIP WAN (GW: $gatewayIP)
|
||||
$gatewayIP = trim(substr(explode(":", $description)[1], 0, -1));
|
||||
printf("CARP Gateway IP from Description: %s \n", $gatewayIP );
|
||||
if (filter_var($gatewayIP, FILTER_VALIDATE_IP)) {
|
||||
foreach ($config['gateways']['gateway_item'] as $i => $gateway) {
|
||||
if ($gateway['name'] == $gatewayName) {
|
||||
printf("Current Gateway IP: %s \n", $gateway['gateway']);
|
||||
if ($gateway['gateway'] != $gatewayIP) {
|
||||
printf("Updating Gateway IPs. \n");
|
||||
$config['gateways']['gateway_item'][$i]['gateway'] = $gatewayIP;
|
||||
write_config_and_restart_services($wanInterface);
|
||||
} else {
|
||||
printf("Gateway IPs are the same, not updating. \n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
printf("Router is currenty carp master, not updating");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
#!/usr/local/bin/php
|
||||
<?php
|
||||
|
||||
require_once("config.inc");
|
||||
require_once("interfaces.inc");
|
||||
require_once("util.inc");
|
||||
require_once("filter.inc");
|
||||
require_once("util.inc");
|
||||
require_once("system.inc");
|
||||
require_once('rrd.inc');
|
||||
require_once('plugins.inc.d/webgui.inc');
|
||||
|
||||
#############
|
||||
# Variables #
|
||||
#############
|
||||
|
||||
$wanInterface = 'wan';
|
||||
$gatewayName = 'WAN_GW';
|
||||
|
||||
###################
|
||||
# Start of Script #
|
||||
###################
|
||||
|
||||
|
||||
function write_config_and_restart_services ($interface) {
|
||||
write_config();
|
||||
system_hosts_generate(true);
|
||||
system_resolvconf_generate(true);
|
||||
interface_bring_down($interface);
|
||||
interface_configure(true, $interface, true);
|
||||
plugins_configure('monitor', true);
|
||||
filter_configure_sync(true);
|
||||
webgui_configure_do(true);
|
||||
rrd_configure(true);
|
||||
}
|
||||
|
||||
# loop through carp addresses and update the wan ip address
|
||||
foreach ($config['virtualip']['vip'] as $i => $vip) {
|
||||
if ($vip['interface'] == $wanInterface) {
|
||||
$description = $config['virtualip']['vip'][$i]['descr'];
|
||||
printf("Description from VIP WAN: %s \n", $description);
|
||||
# VIP WAN (GW: $gatewayIP)
|
||||
$gatewayIP = trim(substr(explode(":", $description)[1], 0, -1));
|
||||
printf("CARP Gateway IP from Description: %s \n", $gatewayIP );
|
||||
if (filter_var($gatewayIP, FILTER_VALIDATE_IP)) {
|
||||
foreach ($config['gateways']['gateway_item'] as $i => $gateway) {
|
||||
if ($gateway['name'] == $gatewayName) {
|
||||
printf("Current Gateway IP: %s \n", $gateway['gateway']);
|
||||
if ($gateway['gateway'] != $gatewayIP) {
|
||||
printf("Updating Gateway IPs. \n");
|
||||
$config['gateways']['gateway_item'][$i]['gateway'] = $gatewayIP;
|
||||
write_config_and_restart_services($wanInterface);
|
||||
} else {
|
||||
printf("Gateway IPs are the same, not updating. \n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user