|
| 1 | +"""The provided sample code in this repository will reference this file to get the |
| 2 | +information needed to connect to your lab backend. You provide this info here |
| 3 | +once and the scripts in this repository will access it as needed by the lab. |
| 4 | +Copyright (c) 2019 Cisco and/or its affiliates. |
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +SOFTWARE. |
| 20 | +""" |
| 21 | + |
| 22 | +# Libraries |
| 23 | +from pprint import pprint |
| 24 | +import sys, os, getopt, json, time, datetime |
| 25 | +from webexteamssdk import WebexTeamsAPI |
| 26 | +import requests |
| 27 | + |
| 28 | +# Get the absolute path for the directory where this file is located "here" |
| 29 | +here = os.path.abspath(os.path.dirname(__file__)) |
| 30 | + |
| 31 | +# Get the absolute path for the project / repository root |
| 32 | +project_root = os.path.abspath(os.path.join(here, "..")) |
| 33 | + |
| 34 | +# Extend the system path to include the project root and import the env files |
| 35 | +sys.path.insert(0, project_root) |
| 36 | +import env_user # noqa |
| 37 | + |
| 38 | +# WEBEX TEAMS LIBRARY |
| 39 | +teamsapi = WebexTeamsAPI(access_token=env_user.WT_ACCESS_TOKEN) |
| 40 | + |
| 41 | +# MERAKI BASE URL |
| 42 | +base_url = "https://api.meraki.com/api/v0" |
| 43 | + |
| 44 | +def getnetworklist(): |
| 45 | + orgs = "" |
| 46 | + |
| 47 | + # Get Orgs that entered Meraki API Key has access to |
| 48 | + try: |
| 49 | + # MISSION TODO |
| 50 | + orgs = requests.get( |
| 51 | + base_url + "TODO:ADD URL TO GET ORGANIZATION LIST HERE", |
| 52 | + headers={ |
| 53 | + "X-Cisco-Meraki-API-Key": env_user.MERAKI_API_KEY, |
| 54 | + } |
| 55 | + ) |
| 56 | + # Deserialize response text (str) to Python Dictionary object so |
| 57 | + # we can work with it |
| 58 | + orgs = json.loads(orgs.text) |
| 59 | + pprint(orgs) |
| 60 | + # END MISSION SECTION |
| 61 | + except Exception as e: |
| 62 | + pprint(e) |
| 63 | + |
| 64 | + # Now get a specific network based on name added on command line |
| 65 | + networks = "" |
| 66 | + if orgs != "": |
| 67 | + for org in orgs: |
| 68 | + try: |
| 69 | + # MISSION TODO |
| 70 | + networks = requests.get( |
| 71 | + base_url + "TODO:ADD URL TO GET NETWORK LIST HERE (be sure to add organization id to the string)", |
| 72 | + headers={ |
| 73 | + "X-Cisco-Meraki-API-Key": env_user.MERAKI_API_KEY, |
| 74 | + }) |
| 75 | + # Deserialize response text (str) to Python Dictionary object so |
| 76 | + # we can work with it |
| 77 | + networks = json.loads(networks.text) |
| 78 | + pprint(networks) |
| 79 | + return networks |
| 80 | + # END MISSION SECTION |
| 81 | + except Exception as e: |
| 82 | + pprint(e) |
| 83 | + return "" |
| 84 | + |
| 85 | + return "No Networks Found" |
| 86 | + |
| 87 | + |
| 88 | +def get_mx_l3_firewall_rules(network): |
| 89 | + # return the MX L3 firewall ruleset for a network |
| 90 | + try: |
| 91 | + # MISSION TODO |
| 92 | + rules = requests.get( |
| 93 | + base_url + "TODO:ADD URL TO GET L3 Firewall Rules HERE (be sure to add network id to the string)", |
| 94 | + headers={ |
| 95 | + "X-Cisco-Meraki-API-Key": env_user.MERAKI_API_KEY, |
| 96 | + }) |
| 97 | + pprint(network + ": " + rules.text) |
| 98 | + return rules.text |
| 99 | + # END MISSION SECTION |
| 100 | + except Exception as e: |
| 101 | + pprint("Rules lookup failed") |
| 102 | + pprint(e) |
| 103 | + return "" |
| 104 | + |
| 105 | +def createbackup(networks): |
| 106 | + # create directory to place backups |
| 107 | + flag_creationfailed = True |
| 108 | + MAX_FOLDER_CREATE_TRIES = 5 |
| 109 | + for i in range(0, MAX_FOLDER_CREATE_TRIES): |
| 110 | + time.sleep(2) |
| 111 | + timestamp = "{:%Y%m%d_%H%M%S}".format(datetime.datetime.now()) |
| 112 | + directory = "mxfwctl_backup_" + timestamp |
| 113 | + flag_noerrors = True |
| 114 | + try: |
| 115 | + os.makedirs(directory) |
| 116 | + except Exception as e: |
| 117 | + print(e) |
| 118 | + flag_noerrors = False |
| 119 | + if flag_noerrors: |
| 120 | + flag_creationfailed = False |
| 121 | + break |
| 122 | + |
| 123 | + if flag_creationfailed: |
| 124 | + pprint("Unable to create directory for backups") |
| 125 | + sys.exit(2) |
| 126 | + else: |
| 127 | + pprint('INFO: Backup directory is ' + directory) |
| 128 | + |
| 129 | + # create backups - one file per network |
| 130 | + for network in networks: |
| 131 | + rules = get_mx_l3_firewall_rules(network["id"]) |
| 132 | + if rules != "": |
| 133 | + filename = network["id"] + ".json" |
| 134 | + filepath = directory + "/" + filename |
| 135 | + if os.path.exists(filepath): |
| 136 | + pprint( |
| 137 | + "Cannot create backup file: name conflict " + filename |
| 138 | + ) |
| 139 | + sys.exit(2) |
| 140 | + else: |
| 141 | + try: |
| 142 | + f = open(filepath, "w") |
| 143 | + except Exception as e: |
| 144 | + pprint( |
| 145 | + "Unable to open file path for writing: " + filepath |
| 146 | + ) |
| 147 | + sys.exit(2) |
| 148 | + |
| 149 | + |
| 150 | + f.write(rules) |
| 151 | + |
| 152 | + try: |
| 153 | + f.close() |
| 154 | + except Exception as e: |
| 155 | + pprintusertext( |
| 156 | + "Unable to close file path: " + filepath |
| 157 | + ) |
| 158 | + sys.exit(2) |
| 159 | + |
| 160 | + pprint( |
| 161 | + "INFO: Created backup for " + network["name"] |
| 162 | + ) |
| 163 | + |
| 164 | + teamsapi.messages.create( |
| 165 | + env_user.WT_ROOM_ID, |
| 166 | + files=[filepath], |
| 167 | + text="Network " + network["name"] + " L3 Rules Backup", |
| 168 | + ) |
| 169 | + else: |
| 170 | + pprint( |
| 171 | + "WARNING: Unable to read MX ruleset for " + network["name"] |
| 172 | + ) |
| 173 | + |
| 174 | + return (0) |
| 175 | + |
| 176 | +# Launch application |
| 177 | +if __name__ == "__main__": |
| 178 | + # Configuration parameters |
| 179 | + networks = getnetworklist() |
| 180 | + createbackup(networks) |
0 commit comments