def get_orgid(apikey): myOrg = meraki.myorgaccess(apikey) for line in myOrg: print(line['name'], 'id: ' + line['id']) orgid = input('Enter Org ID: ') return orgid
def merakiclients(): APIKEY = os.getenv("MERAKI_API_KEY") devices = list() clients = list() mOrgs = meraki.myorgaccess(APIKEY) for org in mOrgs: for net in meraki.getnetworklist(APIKEY, org["id"]): devices += meraki.getnetworkdevices(APIKEY, net["id"]) for device in devices: clients += meraki.getclients(APIKEY, device["serial"]) blobclient = BlobServiceClient.from_connection_string( os.getenv("BLOB_CONNECTION_STRING")) container = blobclient.get_container_client( os.getenv("MERAKI_BLOB_CONTAINER")) blobname = "clients-{}.json".format(datetime.date.today().isoformat()) try: container.upload_blob(blobname, data=json.dumps(clients, indent=2)) except Exception as e: print(e) response = "Uploaded {} containing {} clients".format( blobname, len(clients)) return response
def refresh_Devices(): logging.debug("Cron job to refresh networks was triggered") orgID = meraki.myorgaccess(apikey=apikey) networkList = meraki.getnetworklist(apikey, orgID) if networkList.networkList: logging.debug("network ID list is {}".format( len(networkList.networkIdList))) numberDevices = 0 for net in networkList.networkIdList: logging.debug("fetching devices for {}".format(net)) try: devices = meraki.getnetworkdevices(apikey, net, suppressprint=True) numberDevices += len(devices) except: logging.debug("Cron job to refresh devices failed") return 'Failed to refresh devices', 500 text = "gathered {} devices for all networks in a cron job".format( numberDevices) #slack_client.post_message(channel='#general', text=text) logging.debug(text) return 'Finished refreshing devices', 200 else: text = "Something went wrong refreshing networks from a cron job".format( len(networkList.networkList)) #slack_client.post_message(channel='#general', text=text) logging.debug("Cron job to refresh networks has finished in failure") return 'Failed to refresh networks', 500
def get_org_id(api_key, org_name): orgs = meraki.myorgaccess(api_key, suppressprint=True) id = [x['id'] for x in orgs if x['name'] == org_name] if id: id = id[0] else: raise Exception('Org with name {} could not be found'.format(org_name)) return id
def get_org_id(api_key, org_name): orgs = meraki.myorgaccess(api_key, suppressprint=True) for org in orgs: if org['name'] == org_name: return org['id'] print('Error: Org Not found: ' + org_name) return None
def refresh_Clients(): logging.debug("Cron job to refresh clients was triggered at {}".format( datetime.datetime.now())) orgID = meraki.myorgaccess(apikey=apikey) networkList = meraki.getnetworklist(apikey, orgID) if networkList.networkList: logging.debug("network ID list is {}".format( len(networkList.networkIdList))) numberDevices = 0 numberClients = 0 for net in networkList.networkList: logging.debug("fetching devices for {}".format(net)) networkID = net['id'] networkName = net['name'] logging.debug("{} - {}".format(networkID, networkName)) try: devices = meraki.getnetworkdevices(apikey, net['id'], suppressprint=True) numberDevices += len(devices) logging.debug(len(devices)) for device in devices: try: logging.debug(device['serial']) clientList = meraki.getclients(apikey, device['serial'], timestamp=timespan, force=True, networkId=networkID, networkName=networkName) if clientList.clientList: numberClients += len(clientList.clientList) except: logging.debug( "Cron job to refresh clients failed for device {}". format(device['name'])) pass except: logging.debug( "Cron job to refresh clients failed on getting devices") return 'Failed to refresh clients', 500 text = "gathered {} clients for all networks in a cron job".format( numberClients) #slack_client.post_message(channel='#general', text=text) logging.debug(text) return 'Finished refreshing clients', 200 else: text = "Something went wrong refreshing clients from a cron job" slack_client.post_message(channel='#general', text=text) logging.debug("Cron job to refresh clients has finished in failure") return 'Failed to refresh clients ', 500
def refresh_Networks(): logging.debug("Cron job to refresh networks was triggered") orgID = meraki.myorgaccess(apikey=apikey) networkList = meraki.getnetworklist(apikey, orgID) if networkList.networkList: text = "Refreshed {} networks from a cron job".format( len(networkList.networkList)) #slack_client.post_message(channel='#general', text=text) logging.debug(text) return 'Finished refreshing networks', 200 else: text = "Something went wrong refreshing networks from a cron job".format( len(networkList.networkList)) #slack_client.post_message(channel='#general', text=text) logging.debug(text) return 'Failed to refresh networks', 500
def meraki_getorgs(): """ Called from meraki.html to get a list of Meraki Orgs. This is piped through Python because Dashboard does not support OPTIONS / CORS to do it in JS :return: List of Meraki Organizations in JSON format """ dolog("meraki_getorgs::entrance") apikey = request.headers.get('X-Cisco-Meraki-API-Key') c = meraki.myorgaccess(apikey, suppressprint=True) dolog("meraki_getorgs::" + str(c)) if c: try: j = jsonify(c) return j except: dolog("meraki_getorgs::exception with jsonify/return") else: dolog( "meraki_getorgs::API Key does not have org permission or bad API Key." ) return jsonify([])
def __handle_message_event(self, event): if not self.user_id: self.user_id = self.get_user_id() if event.user_id == self.user_id: logging.debug("Ignoring own message") return if 'meraki' in unicode.lower(event.text): orgID = meraki.myorgaccess(apikey=apikey) #logging.debug("Your Meraki orgId is {}".format(str(orgID))) if 'get networks' in unicode.lower(event.text): networkList= meraki.getnetworklist(apikey,orgID) #logging.debug(networkList.networkList) text = 'you have {} networks in your organisation:\n{}'.format(len(networkList.networkIdList), networkList.networkNameList) self.slack_client.post_message(channel=event.channel, text=text) elif 'get devices' in unicode.lower(event.text): network = unicode.split(unicode.lower(event.text)) if 'devices' in network[-1]: text = "either put 'all' or the name of the network at the end\n e.g. 'meraki get devices london'" self.slack_client.post_message(channel=event.channel, text=text) elif 'all' in network[-1]: networkList = meraki.getnetworklist(apikey, orgID) for net in networkList.networkIdList: devices = meraki.getnetworkdevices(apikey,net,suppressprint=True) text = "gathered {} devices for all networks".format(len(devices)) self.slack_client.post_message(channel=event.channel, text=text) else: networkList = meraki.getnetworklist(apikey, orgID) #logging.debug("trying to find network '{}' in list {}".format(network[-1].lower(), networkList.networkList)) net = [x for x in networkList.networkList if network[-1].lower() in x['name'].lower()] if not net: logging.debug("couldn't find network, aborting") text = "couldn't find network '{}' in our list, please choose from:\n{}".format(network[-1],networkList.networkNameList) self.slack_client.post_message(channel=event.channel, text=text) return else: #logging.debug("found index {} of type {}".format(net, type(net))) networkID = net[0]['id'] #logging.debug("found ID {}".format(networkID)) devices = meraki.getnetworkdevices(apikey, networkID, suppressprint=True) text = "gathered {} devices for network {} ({})".format(len(devices), net[0]['name'],networkID) self.slack_client.post_message(channel=event.channel, text=text) return elif 'get clients' in unicode.lower(event.text): if 'force' in unicode.lower(event.text): force=True else: force=False client = unicode.split(unicode.lower(event.text)) if 'clients' in client[-1]: text = "either put 'all' or the name of a network or WAP at the end\n" \ "e.g. 'meraki get clients network london' or 'merakit get clients WAP11_BSG" self.slack_client.post_message(channel=event.channel, text=text) elif 'all' in client[-1]: networkList = meraki.getnetworklist(apikey, orgID) numberClients = 0 for net in networkList.networkList: networkID = net['id'] networkName = net['name'] devices = meraki.getnetworkdevices(apikey, net['id'], suppressprint=True) for device in devices: clientList = meraki.getclients(apikey,device['serial'], timestamp=timespan, force=force, networkId=networkID, networkName=networkName) if clientList.clientList: numberClients += len(clientList.clientList) text = "gathered {} clients for all devices".format(numberClients) self.slack_client.post_message(channel=event.channel, text=text) return elif 'network' in client: networkList = meraki.getnetworklist(apikey, orgID) #logging.debug("networkList is {}".format(networkList)) #if networkList.networkList: #logging.debug( #"trying to find network '{}' in list {}".format(client[-1].lower(), networkList.networkList)) net = [x for x in networkList.networkList if client[-1].lower() in x['name'].lower()] if not net: logging.debug("couldn't find network, aborting") text = "couldn't find network '{}' in our list, please choose from:\n{}".format(client[-1], networkList.networkNameList) self.slack_client.post_message(channel=event.channel, text=text) else: #logging.debug("found index {} of type {}".format(net, type(net))) networkID = net[0]['id'] networkName = net[0]['name'] logging.debug("found ID {} with name {}".format(networkID, net[0]['name'])) devices = meraki.getnetworkdevices(apikey, networkID, suppressprint=True) #text = "gathering clients from {} devices for network {} ({})".format(len(devices), net[0]['name'], networkID) #self.slack_client.post_message(channel=event.channel, text=text) clientListLen = 0 for device in devices: clientList = meraki.getclients(apikey, device['serial'], timestamp=timespan, force=force, networkId=networkID, networkName=networkName) if clientList.clientList: clientListLen += len(clientList.clientList) self.slack_client.post_message(channel=event.channel, text= "gathered {} clients for {}".format(str(clientListLen),networkName)) return elif 'get client' in unicode.lower(event.text): client = unicode.split(unicode.lower(event.text)) if 'client' in client[-1]: text = "either put 'ip x.x.x.x' or 'mac xxxx.xxxx.xxxx.xxxx'" self.slack_client.post_message(channel=event.channel, text=text) elif 'mac' in client[-2]: clientresult = meraki_models.MerakiClient.query().filter(meraki_models.MerakiClient.mac == client[-1]) result = clientresult.fetch(1) if len(result) > 0: logging.debug(result[0]) tempclient = { 'mac': result[0].mac, 'ip': result[0].ip, 'networkName': result[0].networkName, 'description': result[0].description, 'client_id': result[0].client_id } msg="client ID {} found in network {}\nwith IP address {}\n" \ "the description is {}".format(tempclient['client_id'], tempclient['networkName'], tempclient['ip'], tempclient['description']) buttons = [ { "text": "Choose an action for this client", "fallback": "Couldn't display action buttons", "callback_id": "client_action", "attachment_type": "default", "actions": [ { "name": "action", "type": "button", "text": "block", "style": "danger", "value": "block {}".format(result[0].mac), "confirm": { "title": "Are you sure?", "text": "Really block {}?".format(result[0].mac), "ok_text": "Yes", "dismiss_text": "No" } }, { "name": "action", "type": "button", "text": "unblock", "value": "unblock {}".format(result[0].mac) }, { "name": "action", "type": "button", "text": "de-authorize", "style": "danger", "value": "de-authorize {}?".format(result[0].mac), "confirm": { "title": "Are you sure?", "text": "Really de-auth {}".format(result[0].mac), "ok_text": "Yes", "dismiss_text": "No" } }, { "name": "action", "type": "button", "text": "re-authorize", "value": "re-authorize {}".format(result[0].mac) } ] } ] logging.debug(json.loads(json.dumps(buttons))) self.slack_client.post_message(channel=event.channel, text=msg, attachments=json.dumps(buttons)) return else: msg = "Sorry I couldn't find client with MAC {}".format(client[-1]) self.slack_client.post_message(channel=event.channel, text=msg) return elif 'ip' in client[-2]: clientresult = meraki_models.MerakiClient.query().filter( meraki_models.MerakiClient.ip == client[-1]).order(meraki_models.MerakiClient.timestamp) result = clientresult.fetch(20) logging.debug(result) clientresult2 = meraki_models.MerakiClient.query().filter( meraki_models.MerakiClient.ip == client[-1]).order(-meraki_models.MerakiClient.timestamp) result2 = clientresult2.fetch(20) logging.debug(result2) if len(result) > 0: tempclient = { 'mac': result[0].mac, 'ip': result[0].ip, 'networkName': result[0].networkName, 'description': result[0].description, 'client_id': result[0].client_id } msg="client ID {} found in network {}\nwith MAC address {}\n" \ "the description is {}".format(tempclient['client_id'], tempclient['networkName'], tempclient['mac'], tempclient['description']) buttons = [ { "text": "Choose an action for this client", "fallback": "Couldn't display action buttons", "callback_id": "client_action", "attachment_type": "default", "actions": [ { "name": "action", "type": "button", "text": "block", "style": "danger", "value": "block {}".format(result[0].mac), "confirm": { "title": "Are you sure?", "text": "Really block {}?".format(result[0].mac), "ok_text": "Yes", "dismiss_text": "No" } }, { "name": "action", "type": "button", "text": "unblock", "value": "unblock {}".format(result[0].mac) }, { "name": "action", "type": "button", "text": "de-authorize", "style": "danger", "value": "de-authorize {}".format(result[0].mac), "confirm": { "title": "Are you sure?", "text": "Really de-auth {}?".format(result[0].mac), "ok_text": "Yes", "dismiss_text": "No" } }, { "name": "action", "type": "button", "text": "re-authorize", "value": "re-authorize {}".format(result[0].mac) } ] } ] #logging.debug(json.loads(json.dumps(buttons))) self.slack_client.post_message(channel=event.channel, text=msg, attachments=json.dumps(buttons)) return else: msg = "Sorry I couldn't find client with IP {}".format(client[-1]) self.slack_client.post_message(channel=event.channel, text=msg) return elif 'unblock client' in unicode.lower(event.text): client = unicode.split(unicode.lower(event.text)) if 'client' in client[-1]: text = "put 'unblock client mac xxxx.xxxx.xxxx.xxxx'" self.slack_client.post_message(channel=event.channel, text=text) return elif 'mac' in client[-2]: clientresult = meraki_models.MerakiClient.query().filter( meraki_models.MerakiClient.mac == client[-1]) result = clientresult.fetch(1) logging.debug(result[0]) if result: tempclient = { 'mac': result[0].mac, 'ip': result[0].ip, 'networkName': result[0].networkName, 'description': result[0].description, 'client_id': result[0].client_id } result = meraki.updateclientpolicy(apikey,result[0].networkId, result[0].mac,policy='normal') logging.debug(result) msg = "client ID {} unblocked ".format(tempclient['client_id']) self.slack_client.post_message(channel=event.channel, text=msg) return # msg = "Sorry something went wrong with unblocking the client" # self.slack_client.post_message(channel=event.channel, text=msg) # return else: msg = "Sorry I couldn't find client with MAC {}".format(client[-1]) self.slack_client.post_message(channel=event.channel, text=msg) return else: msg = "Couldn't understand the unblock request" logging.debug(msg) self.slack_client.post_message(channel=event.channel, text=msg) return elif 'block client' in unicode.lower(event.text): client = unicode.split(unicode.lower(event.text)) logging.debug("{}{}{}{}".format(client[-1],client[-2],client[-3],client[-4])) if 'client' in client[-1]: text = "put 'mac xxxx.xxxx.xxxx.xxxx ticket yyyyy'" self.slack_client.post_message(channel=event.channel, text=text) return elif 'mac' in client[-4]: clientresult = meraki_models.MerakiClient.query().filter( meraki_models.MerakiClient.mac == client[-3]) result = clientresult.fetch(1) logging.debug(result[0]) if result: tempclient = { 'mac': result[0].mac, 'ip': result[0].ip, 'networkName': result[0].networkName, 'description': result[0].description, 'client_id': result[0].client_id } splashblock = blockmsg + client[-1] logging.debug(splashblock) result = meraki.updateclientpolicy(apikey,result[0].networkId, result[0].mac,policy='blocked') logging.debug(result) msg = "client ID {} blocked ".format(tempclient['client_id']) logging.debug(msg) self.slack_client.post_message(channel=event.channel, text=msg) return # msg = "Sorry something went wrong with blocking the client" # self.slack_client.post_message(channel=event.channel, text=msg) # return else: msg = "Sorry I couldn't find client with MAC {}".format(client[-1]) self.slack_client.post_message(channel=event.channel, text=msg) return else: msg = "Couldn't understand the block request" logging.debug(msg) self.slack_client.post_message(channel=event.channel, text=msg) return elif 'unauthorize client' in unicode.lower(event.text): client = unicode.split(unicode.lower(event.text)) if 'client' in client[-1]: text = "put 'unauthorize client mac xxxx.xxxx.xxxx.xxxx'" self.slack_client.post_message(channel=event.channel, text=text) return elif 'mac' in client[-2]: clientresult = meraki_models.MerakiClient.query().filter( meraki_models.MerakiClient.mac == client[-1]) result = clientresult.fetch(1) logging.debug(result[0]) if result: tempclient = { 'mac': result[0].mac, 'ip': result[0].ip, 'networkName': result[0].networkName, 'description': result[0].description, 'client_id': result[0].client_id } result = meraki.updateclientpolicy(apikey,result[0].networkId, result[0].mac,policy='normal') logging.debug(result) msg = "client ID {} unblocked ".format(tempclient['client_id']) self.slack_client.post_message(channel=event.channel, text=msg) return # msg = "Sorry something went wrong with unblocking the client" # self.slack_client.post_message(channel=event.channel, text=msg) # return else: msg = "Sorry I couldn't find client with MAC {}".format(client[-1]) self.slack_client.post_message(channel=event.channel, text=msg) return else: msg = "Couldn't understand the unblock request" logging.debug(msg) self.slack_client.post_message(channel=event.channel, text=msg) return else: msg = 'Hi, I did not understand your message ' + event.text
#! python3 # printOrgs.py - Prints a pretty table of associated Meraki orgs. import meraki import pprint print('Enter your API key:') apikey = input() print('You entered: ' + apikey) print('\nmeraki.myorgaccess') myOrgs = meraki.myorgaccess(apikey) print(myOrgs) print('\npprint meraki.myorgaccess') pprint.pprint(meraki.myorgaccess(apikey)) print('Enter the Org ID:') orgid = input() print('You entered: ' + orgid) #orgid = 756963 print('\nmeraki.mygetnetworklist') pprint.pprint(meraki.getnetworklist(apikey, orgid))