Last active
May 18, 2022 20:58
-
-
Save marria05/da183d4aae38c43327e11a75f8eab11c to your computer and use it in GitHub Desktop.
Create a Mikrotik address list from the AWS 'ip-ranges' API
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/python3 | |
# -*- coding: iso-8859-1 -*- | |
# Launch with AWS region name as the only command line argument ie. ./mikrotik_aws_addresslists.py eu-west-1 | |
# A list of addresses, along with requisite terminal commands prepended to each line is spewed forth from stdout, | |
# which can be redirected to a file. | |
import signal | |
import json | |
import requests | |
import sys | |
#================================================================================================================== | |
def interruptHandler(signum, frame): | |
exit() | |
return | |
def main(): | |
response = requests.Session().get("https://ip-ranges.amazonaws.com/ip-ranges.json") | |
if response.status_code != 200: | |
return | |
print(str.join('\n', ['/ip firewall address-list add list=' + sys.argv[1] + ' address=' + row['ip_prefix'] for row in response.json()['prefixes'] if row['region'] == sys.argv[1]])) | |
return | |
#================================================================================================================== | |
if __name__ == "__main__": | |
signal.signal(signal.SIGINT, interruptHandler) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment