carlessanagustin
6/20/2018 - 3:24 PM

Generate a list of StatusCake IP addresses to allow in firewall

Generate a list of StatusCake IP addresses to allow in firewall

#!/usr/bin/env python3

import requests
import json
import subprocess

debug=False
url = 'https://app.statuscake.com/Workfloor/Locations.php?format=json'
comment = 'statuscake.com'
command = 'sudo iptables '

def add_rules( url, comment, command ):
    print ('** Adding...')
    global json
    content = requests.get(url)
    json = json.loads(content.content)
    for item in json:
        rule = command + '-I INPUT -s ' + json[item]['ip'] + '/32 -p tcp -m tcp --dport 80 -m comment --comment "' + comment + '" -j ACCEPT'
        if debug: print (rule)
        if not debug: subprocess.run(rule, shell=True)

def delete_rules( comment, command ):
    print ('** Deleting...')
    rules = command + '-S | grep "' + comment + '"'
    try:
        output = subprocess.check_output(rules,shell=True,stderr=subprocess.STDOUT)
        for line_b in output.splitlines():
            line_l = list (line_b.decode("utf-8"))
            line_l[1] = 'D'
            rule = command + ''.join(line_l)
            if debug: print (rule)
            if not debug: subprocess.run(rule, shell=True)
    except subprocess.CalledProcessError:
        pass

delete_rules( comment, command )
add_rules( url, comment, command )
<?php
/**
 * Generate a list of Status Cake Ip addresses 
 * To add allowed IPs to Nginx or Firewall
 * from: https://www.statuscake.com/kb/knowledge-base/what-are-your-ips/
 */

$statuscake_locations_json = "https://app.statuscake.com/Workfloor/Locations.php?format=json";
$locations = json_decode(file_get_contents($statuscake_locations_json));
$output = "#STATUS CAKE IPs" . "\n";

/**
 * Formatted for Nginx
 */
foreach($locations as $location) {
 $output .= "allow " . $location->ip . ";" . "\n";
}

print_r($output);