#!/usr/bin/perl -w use strict; use LWP; use JSON; use Getopt::Long; use File::Compare; use File::Copy; my ($key, $id) = ''; GetOptions( "key=s" => \$key, "id=s" => \$id ); if ($key eq '' || $id eq ''){ print "Usage: $0 --key= --id=\n"; exit(1); } # Used to hide api key $0 = 'failover updater'; my $c = LWP::UserAgent->new; my $r = $c->get("https://api.online.net/api/v1/server/info/$id", "Authorization" => "Authorization: Bearer $key", ); unless ($r->is_success){ print "an error occured while querying the API" . "The error is: " . $r->status_line; exit(2); } my $data = from_json($r->content); open TMP, ">/tmp/proxyarp" || die "Cannot open /tmp/proxyarp"; # Print the file header print TMP <<"EOF"; ############################################################################### ##ADDRESS\t\tINTERFACE\tEXTERNAL\tHAVEROUTE\tPERSISTENT EOF # Now add one arp proxy rule per failover IP # redirected to ourself foreach my $ip ( @{$data->{'network'}->{'ipfo'}} ){ print TMP "$ip\t\tbrwan\t\teth0\t\tNo\t\tYes\n"; } close TMP; if (compare("/etc/shorewall/proxyarp", "/tmp/proxyarp") != 0){ # Looks like IP Failover changed, we need to update shorewall # # config, reload it and flush the ARP cache print "Updating ARP Proxy config and flushing cache...\n"; move ("/tmp/proxyarp", "/etc/shorewall/proxyarp"); system("/sbin/restorecon -R /etc/shorewall >/dev/null 2>&1"); # Wait a few second for the new routing rules to take place # It will result in more down time if we update too early sleep(90); system("/etc/init.d/wan restart >/dev/null 2>&1"); system("/etc/init.d/shorewall reload >/dev/null 2>&1"); } else{ unlink "/tmp/proxyarp"; }