Created
November 1, 2017 16:26
-
-
Save bradschm/2ec08e2db51735d0d3f5df93dba46a7b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import requests # Needs to be installed | |
| import json | |
| import smtplib | |
| from email.mime.text import MIMEText | |
| import os | |
| FILENAME = 'aws_ips.json' # local storage for comparison | |
| SMTP_SERVER = 'smtp.potato.local' | |
| SEND_FROM = 'from@example.com' | |
| SEND_TO = 'to@example.com' | |
| SITE_ROOT = os.path.dirname(os.path.realpath(__file__)) | |
| def main(): | |
| '''Compare changes to the IP address ranges that Amazon uses for us-east-1 and 2''' | |
| def compare_ips(): | |
| new_addresses = [] | |
| for i in new['ip_addresses']: | |
| if i not in old['ip_addresses']: | |
| new_addresses.append(i) | |
| if len(new_addresses) == 0: | |
| print "Something was removed" | |
| exit() | |
| return new_addresses | |
| def send_notice(new_addresses): | |
| try: | |
| message_text = """The AWS IP Address Block has changed! | |
| Please add these to the Securly Firewall Rules.\n | |
| Date Created:\t%s | |
| Last Modified:\t%s\n | |
| New IP Addresses:\n%s""" | |
| msg = MIMEText(message_text.replace('\t\t\t','') | |
| % (new['createDate'],old['createDate'],'\n'.join(new_addresses)) | |
| ) | |
| msg['Subject'] = '[Warning] AWS IP Range Change!' | |
| msg['From'] = SEND_FROM | |
| msg['To'] = SEND_TO | |
| except: | |
| print "something went wrong with mail!" | |
| exit() | |
| s = smtplib.SMTP(SMTP_SERVER) | |
| s.sendmail(SEND_FROM,SEND_TO, msg.as_string()) | |
| s.quit | |
| # Read previous values | |
| try: | |
| with open('%s/%s' % (SITE_ROOT,FILENAME), 'r') as f: | |
| old = json.loads(f.read()) | |
| except: | |
| print "File not found or file not json." | |
| # Get new json file from AWS | |
| r = requests.get('https://ip-ranges.amazonaws.com/ip-ranges.json') | |
| # Test that we can connect to the json file | |
| try: | |
| r.raise_for_status() | |
| except requests.exceptions.HTTPError as e: | |
| print(e.message) | |
| sys.exit(1) | |
| ranges = r.json() | |
| # This dictionary will hold the IP ranges from us-east-1 and us-east-2 | |
| new = {} | |
| new['ip_addresses'] = [] | |
| # for testing | |
| # new['ip_addresses'].append('13.1.1.01/24') | |
| # new['ip_addresses'].append('10.12.1.92/28') | |
| # new['ip_addresses'].append('13.1.11.12/14') | |
| # Add the createDate - for the email | |
| new['createDate'] = ranges['createDate'] | |
| # Add IPs to list within dictionary | |
| for i in ranges['prefixes']: | |
| if i['region'] == 'us-east-1' or i['region'] == 'us-east-1': | |
| new['ip_addresses'].append(i['ip_prefix']) | |
| try: | |
| # Test to see if we read the old file in | |
| if new == old: | |
| print "No Range Changes Detected!" | |
| else: | |
| send_notice(compare_ips()) | |
| except: | |
| print "Nothing to compare - First Run or something was removed" | |
| # Write current values | |
| with open('%s/%s' % (SITE_ROOT,FILENAME), 'w') as f: | |
| json.dump(new, f) | |
| if __name__ == '__main__': | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment