Compare commits

...

2 Commits

2 changed files with 133 additions and 121 deletions

View File

@@ -68,13 +68,13 @@ echo_write_log("Name of static WAN Gateway: " . $gatewayName);
$intf_details = legacy_interfaces_details();
$intf = get_real_interface($wanInterface);
echo_write_log("Real WAN Interface: " . $intf);
echo_write_log("Looping through VIP Carp Interfaces...");
# loop through carp addresses and update the wan ip address
echo_write_log("Looping through VIP CARP Interfaces...");
# loop through CARP addresses and update the wan ip address
foreach ($config['virtualip']['vip'] as $i => $vip) {
if ($vip['interface'] == $wanInterface) {
echo_write_log("VIP Carp WAN Interface and vhid: " . convert_friendly_interface_to_friendly_descr($vip['interface']) . "@{$vip['vhid']}");
echo_write_log("VIP CARP WAN Interface and vhid: " . convert_friendly_interface_to_friendly_descr($vip['interface']) . "@{$vip['vhid']}");
$carpStatus = $intf_details[$intf]['carp'][$vip['vhid']]['status'];
echo_write_log("VIP Carp WAN Status: " . $carpStatus);
echo_write_log("VIP CARP WAN Status: " . $carpStatus);
if ($carpStatus == "BACKUP") {
$description = $config['virtualip']['vip'][$i]['descr'];
echo_write_log("Description from VIP WAN: " . $description);
@@ -100,7 +100,7 @@ foreach ($config['virtualip']['vip'] as $i => $vip) {
echo_write_log("Gateway IP is not a valid IP address");
}
} else {
echo_write_log("Router is currently Carp master, not updating");
echo_write_log("Router is currently CARP master, not updating");
}
}
}

View File

@@ -31,9 +31,6 @@ $gatewayName = 'WAN_GW';
$wanStaticIP = '192.168.101.1';
$wanStaticIPSubnet = '30';
# Backup Node/Router
$syncIP = $config['hasync']['synchronizetoip'];
###################
# Start of Script #
###################
@@ -84,130 +81,145 @@ echo_write_log("Name of static WAN Gateway: " . $gatewayName);
echo_write_log("Static WAN IP: " . $wanStaticIP);
echo_write_log("Static WAN Subnit Bits: " . $wanStaticIPSubnet);
printf("WAN Interface Name: %s \n", $wanInterface);
printf("Gateway Name: %s \n", $gatewayName);
printf("WAN Static IP: %s \n", $wanStaticIP);
printf("WAN Subnet: %s \n", $wanStaticIPSubnet);
printf("Sync IP: %s \n", $syncIP);
do {
echo_write_log("Setting WAN to DHCP");
$vip = $config['virtualip']['vip'];
$intf_details = legacy_interfaces_details();
$intf = get_real_interface($wanInterface);
echo_write_log("Real WAN Interface: " . $intf);
echo_write_log("Looping through VIP carps to get WAN status...");
foreach ($vip as $carp) {
if ($carp['interface'] == $wanInterface) {
echo_write_log("VIP CARP WAN Interface and vhid: " . convert_friendly_interface_to_friendly_descr($carp['interface']) . "@{$carp['vhid']}");
$wanCarp = $carp;
break;
}
}
# CARP Status MASTER or BACKUP
$carpStatus = $intf_details[$intf]['carp'][$wanCarp['vhid']]['status'];
echo_write_log("VIP CARP WAN Status: " . $carpStatus);
if ($carpStatus == "MASTER") {
do {
echo_write_log("Setting WAN to DHCP");
# Config for WAN DHCP and disable static gateway
$config['interfaces'][$wanInterface]['ipaddr'] = 'dhcp';
$config['interfaces'][$wanInterface]['subnet'] = '';
$config['interfaces'][$wanInterface]['gateway'] = '';
foreach ($config['gateways']['gateway_item'] as $i => $gateway) {
if ($gateway['name'] == $gatewayName) {
unset($config['gateways']['gateway_item'][$i]);
# Config for WAN DHCP and disable static gateway
$config['interfaces'][$wanInterface]['ipaddr'] = 'dhcp';
$config['interfaces'][$wanInterface]['subnet'] = '';
$config['interfaces'][$wanInterface]['gateway'] = '';
foreach ($config['gateways']['gateway_item'] as $i => $gateway) {
if ($gateway['name'] == $gatewayName) {
unset($config['gateways']['gateway_item'][$i]);
}
}
# set the config and reload everything to get a working DHCP
write_config_and_restart_services($wanInterface);
echo_write_log("Done setting WAN to DHCP");
# DHCP ip address and gateway address loop
echo_write_log("Getting DHCP and Gateway IP Addresses");
# get DHCP wan address and subnet
$iflist = legacy_config_get_interfaces(array('enable' => true, 'virtual' => false));
$ifdetails = legacy_interfaces_details();
if (!count($iflist)) {
echo "\n\tNo network interfaces are assigned.\n";
return;
}
$wanInfo = null;
foreach ($iflist as $ifname => $ifcfg) {
$class = null;
if ($ifcfg['descr'] == 'WAN') {
# this will have all the information about the WAN interface
$ifcfg['details'] = $ifdetails[$ifcfg['if']];
# this will break out the loop and leave the $ifcfg with the details of the wan
break;
#print_r($ifcfg);
}
}
$wanIP = $ifcfg['details']['ipv4'][0]['ipaddr'];
$wanSubnet = $ifcfg['details']['ipv4'][0]['subnetbits'];
echo_write_log("WAN DHCP IP: " . $wanIP);
echo_write_log("WAN DHCP Sbunet Bits: " . $wanSubnet);
$gatewayIP = trim(shell_exec('netstat -rn | grep default | awk \'{print $2;}\''));
echo_write_log("Gateway IP: " . $gatewayIP);
echo_write_log("Done getting DHCP and Gateway IP Addresses");
if (filter_var($wanIP, FILTER_VALIDATE_IP) and is_int($wanSubnet) and filter_var($gatewayIP, FILTER_VALIDATE_IP)) {
echo_write_log("WAN IP, Subnet Bits, and Gateway IP are okay");
break;
} else {
echo_write_log("Warning: Missing WAN IP, Subnet Bits, or Gateway IP. Trying again...");
}
} while (true);
echo_write_log("Setting WAN to Static based on the DHCP information");
# Config for WAN Static
$config['interfaces'][$wanInterface]['ipaddr'] = $wanStaticIP;
$config['interfaces'][$wanInterface]['subnet'] = $wanStaticIPSubnet;
$config['interfaces'][$wanInterface]['gateway'] = $gatewayName;
# setup gateway
$gwCount = count($config['gateways']['gateway_item']);
$config['gateways']['gateway_item'][$gwCount]['gateway'] = $gatewayIP;
$config['gateways']['gateway_item'][$gwCount]['interface'] = $wanInterface;
$config['gateways']['gateway_item'][$gwCount]['name'] = $gatewayName;
$config['gateways']['gateway_item'][$gwCount]['ipprotocol'] = 'inet';
$config['gateways']['gateway_item'][$gwCount]['monitor_disable'] = 1;
$config['gateways']['gateway_item'][$gwCount]['defaultgw'] = 1;
$config['gateways']['gateway_item'][$gwCount]['fargw'] = 1;
# Loop through outbound nat and update the wan ip
foreach ($config['nat']['outbound']['rule'] as $i => $rule) {
if ($rule['interface'] == $wanInterface ) {
$config['nat']['outbound']['rule'][$i]['target'] = $wanIP;
}
}
# loop through CARP addresses and update the wan ip address
foreach ($config['virtualip']['vip'] as $i => $vip) {
if ($vip['interface'] == $wanInterface) {
$config['virtualip']['vip'][$i]['subnet_bits'] = $wanSubnet;
$config['virtualip']['vip'][$i]['subnet'] = $wanIP;
$config['virtualip']['vip'][$i]['descr'] = "VIP WAN (GW: $gatewayIP)";
}
}
# set the config and reload everything to get a working DHCP
write_config_and_restart_services($wanInterface);
echo_write_log("Done setting WAN to DHCP");
echo_write_log("Done Setting WAN to Static based on the DHCP information");
} else {
echo_write_log("Router is not currently CARP master, not updating");
}
echo_write_log("Testing to see if this router is the HA sync master");
if (filter_var($config['hasync']['synchronizetoip'], FILTER_VALIDATE_IP)) {
echo_write_log("Sleeping for 30 seconds");
sleep(30);
# DHCP ip address and gateway address loop
echo_write_log("Getting DHCP and Gateway IP Addresses");
# get DHCP wan address and subnet
$iflist = legacy_config_get_interfaces(array('enable' => true, 'virtual' => false));
$ifdetails = legacy_interfaces_details();
if (!count($iflist)) {
echo "\n\tNo network interfaces are assigned.\n";
return;
}
$wanInfo = null;
foreach ($iflist as $ifname => $ifcfg) {
$class = null;
if ($ifcfg['descr'] == 'WAN') {
# this will have all the information about the WAN interface
$ifcfg['details'] = $ifdetails[$ifcfg['if']];
# this will break out the loop and leave the $ifcfg with the details of the wan
# try to sync CARP settings to backup node/router
echo_write_log("Starting the CARP Sync");
echo_write_log("Trying to ping sync host, " . $config['hasync']['synchronizetoip']);
do {
exec("ping -c 4 " . $config['hasync']['synchronizetoip'], $output, $result);
if ($result == 0) {
echo_write_log("Ping was successful");
break;
#print_r($ifcfg);
} else {
echo_write_log("Ping was unsuccessful");
}
}
} while (true)
$wanIP = $ifcfg['details']['ipv4'][0]['ipaddr'];
$wanSubnet = $ifcfg['details']['ipv4'][0]['subnetbits'];
echo_write_log("WAN DHCP IP: " . $wanIP);
echo_write_log("WAN DHCP Sbunet Bits: " . $wanSubnet);
$gatewayIP = trim(shell_exec('netstat -rn | grep default | awk \'{print $2;}\''));
echo_write_log("Gateway IP: " . $gatewayIP);
echo_write_log("Done getting DHCP and Gateway IP Addresses");
if (filter_var($wanIP, FILTER_VALIDATE_IP) and is_int($wanSubnet) and filter_var($gatewayIP, FILTER_VALIDATE_IP)) {
echo_write_log("WAN IP, Subnet Bits, and Gateway IP are okay");
break;
} else {
echo_write_log("Warning: Missing WAN IP, Subnet Bits, or Gateway IP. Trying again...");
}
} while (true);
exec('configctl system ha_reconfigure_backup', $output, $result);
echo_write_log("Setting WAN to Static based on the DHCP information");
# Config for WAN Static
$config['interfaces'][$wanInterface]['ipaddr'] = $wanStaticIP;
$config['interfaces'][$wanInterface]['subnet'] = $wanStaticIPSubnet;
$config['interfaces'][$wanInterface]['gateway'] = $gatewayName;
# setup gateway
$gwCount = count($config['gateways']['gateway_item']);
$config['gateways']['gateway_item'][$gwCount]['gateway'] = $gatewayIP;
$config['gateways']['gateway_item'][$gwCount]['interface'] = $wanInterface;
$config['gateways']['gateway_item'][$gwCount]['name'] = $gatewayName;
$config['gateways']['gateway_item'][$gwCount]['ipprotocol'] = 'inet';
$config['gateways']['gateway_item'][$gwCount]['monitor_disable'] = 1;
$config['gateways']['gateway_item'][$gwCount]['defaultgw'] = 1;
$config['gateways']['gateway_item'][$gwCount]['fargw'] = 1;
# Loop through outbound nat and update the wan ip
foreach ($config['nat']['outbound']['rule'] as $i => $rule) {
if ($rule['interface'] == $wanInterface ) {
$config['nat']['outbound']['rule'][$i]['target'] = $wanIP;
}
echo_write_log("CARP Sync Status: " . $output);
echo_write_log("finished the CARP Sync");
} else {
echo_write_log("Router is not the HA sync master");
}
# loop through carp addresses and update the wan ip address
foreach ($config['virtualip']['vip'] as $i => $vip) {
if ($vip['interface'] == $wanInterface) {
$config['virtualip']['vip'][$i]['subnet_bits'] = $wanSubnet;
$config['virtualip']['vip'][$i]['subnet'] = $wanIP;
$config['virtualip']['vip'][$i]['descr'] = "VIP WAN (GW: $gatewayIP)";
}
}
# set the config and reload everything to get a working DHCP
write_config_and_restart_services($wanInterface);
echo_write_log("Done Setting WAN to Static based on the DHCP information");
echo_write_log("Sleeping for 30 seconds");
sleep(30);
# try to sync carp settings to backup node/router
echo_write_log("Starting the CARP Sync");
echo_write_log("Trying to ping sync host, " . $syncIP);
do {
exec("ping -c 4 " . $syncIP, $output, $result);
if ($result == 0) {
echo_write_log("Ping is successful");
break;
} else {
echo_write_log("Ping unsuccessful");
}
} while (true)
echo_write_log("Sleeping for 30 seconds");
sleep(30);
$syncStatus = shell_exec('configctl system ha_reconfigure_backup');
echo_write_log("CARP Sync Status: " . $syncStatus);
echo_write_log("finished the CARP Sync");
echo_write_log("End of Script " . $argv[0]);