Last active
August 20, 2023 21:17
-
-
Save carlessanagustin/526298ea7e9cc8554cf4c6ce3d9eedb2 to your computer and use it in GitHub Desktop.
Generate a list of StatusCake IP addresses to allow in firewall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nifty scripts, thanks for having this up