def get_ap_list(api_key, net_id): """ This function uses requests to GET a network's devices and creates a list of MR's """ # creates the dictionary called net_devices to store key,value pairs serial_list = [] result = merakiapi.getnetworkdevices(api_key, net_id) for row in result: if row == 'errors': return 'errors' else: # iterate through the json response from the GET inventory model = row['model'] if model[:2] == 'MR': serial_list.append(row['serial']) else: continue return serial_list
def get_guest_ap_list(api_key, net_id): """ This function uses requests to GET a network's devices and creates a list of MR's with the 'guest_wireless' device tag """ # creates the dictionary called net_devices to store key,value pairs serial_list = [] result = merakiapi.getnetworkdevices(api_key, net_id) for row in result: if row == 'errors': return 'errors' else: # iterate through the json response from the GET inventory guest_regex = re.compile('guest_wireless') m = guest_regex.search(str(row['tags'])) model = row['model'] if model[:2] == 'MR' and m is not None: serial_list.append(row['serial']) else: continue return serial_list
if key == 'name': net_name = value elif key == 'id': net_id = value elif key == 'tags': net_tags = value else: continue if tag in net_tags: net_id_list.append(net_id) print ("NETWORKS TO MODIFY") print (net_id_list) for net in net_id_list: per_net_devices = merakiapi.getnetworkdevices(api_key,net) for d in per_net_devices: for key,value in d.items(): if key == 'name': device_name = str(value) elif key == 'serial': device_serial = str(value) elif key == 'model': device_model = str(value) else: continue # For combined networks, only look at MX if 'MS' in device_model: device_id_list.append(device_serial) print ("SWITCHES TO MODIFY")
if os.path.isfile(bncfile): writefile = open(bncfile, 'a+') else: writefile = open(bncfile, 'w+') print('Network name,Serial,Network tags,Name,Tags,Address,Notes,Static IP,Netmask,Gateway,DNS1,DNS2,VLAN', file=writefile) orgnetworks = merakiapi.getnetworklist(apikey, org, suppressprint=True) for network in orgnetworks: networkname = network['name'] if 'tags' not in network: networktags = '' else: networktags = network['tags'] devicelist = merakiapi.getnetworkdevices(apikey, network['id'], suppressprint=True) for device in devicelist: if 'serial' not in device: serialnum = '' else: serialnum = device['serial'] if 'name' not in device: devicename = '' else: devicename = device['name'] if 'model' not in device: devicemodel = '' else: devicemodel = device['model'] if 'lanIp' not in device: deviceip = ''
def main(): # read from xlsx file configurations = {} from openpyxl import load_workbook wb = load_workbook(filename='template.xlsx') first_sheet = wb.get_sheet_names()[0] ws = wb[first_sheet] my_row = [] for i in range(1, ws.max_row - 2): for j in range(1, ws.max_column - 1): my_row.append(ws.cell(row=i + 1, column=j).value) #configurations[ws.cell(row=i + 1, column=2).value + str(ws.cell(row=i + 1, column=3).value)] = SwitchPort(my_row) # dictionary print( ws.cell(row=i + 1, column=2).value + str(ws.cell(row=i + 1, column=3).value)) print(my_row) print(configurations) # API key. api_key = "8b43aaa7b92b6d3ad06234e6f581077620d3e512" # Get the organization name. print("Organization Name:") org_name = input() # Pull the organizations associated to the provided API key. orgs = merakiapi.myorgaccess(api_key, True) # Look for the organization that we want to configure. org_id = "" for org in orgs: if org_name in org["name"]: org_id = org["id"] if org_id == "": print("Organization not found.") return # Pull the networks associated with the organization. networks = merakiapi.getnetworklist(api_key, org_id, True) # Pull the devices from all of the networks. devices = [] for network in networks: devices += merakiapi.getnetworkdevices(api_key, network["id"], True) # print devices switch_ports = [] for device in devices: current_switch_ports = [] if device["model"].startswith("MS"): # current_switch_port = merakiapi.getswitchports(api_key, device["serial"]) # current_switch_port["serial"] = device["serial"] current_switch_ports = merakiapi.getswitchports( api_key, device["serial"], False) # Label all current switch ports with the serial number of the parent switch. for switch_port in current_switch_ports: switch_port["serial"] = device["serial"] # Append the switch ports for the current switch to the master list. switch_ports += current_switch_ports print(switch_ports) # Apply configuration to the devices and push them to Meraki. for switch_port in switch_ports: try: switch_port["name"] = configurations[ switch_port["serial"] + str(switch_port["number"])].name except: continue switch_port["tags"] = configurations[switch_port["serial"] + str(switch_port["number"])].tags switch_port["enabled"] = configurations[ switch_port["serial"] + str(switch_port["number"])].enabled switch_port["rstpEnabled"] = configurations[ switch_port["serial"] + str(switch_port["number"])].rstp switch_port["stpGuard"] = configurations[ switch_port["serial"] + str(switch_port["number"])].stp_guard switch_port["poeEnabled"] = configurations[ switch_port["serial"] + str(switch_port["number"])].poe switch_port["type"] = configurations[switch_port["serial"] + str(switch_port["number"])].type switch_port["vlan"] = configurations[switch_port["serial"] + str(switch_port["number"])].vlan switch_port["voiceVlan"] = configurations[ switch_port["serial"] + str(switch_port["number"])].voice_vlan switch_port["allowedVlans"] = configurations[ switch_port["serial"] + str(switch_port["number"])].allowed_vlan # print switch_port["enabled"] merakiapi.updateswitchport( api_key, switch_port["serial"], switch_port["number"], switch_port["name"], switch_port["tags"], switch_port["enabled"], switch_port["type"], switch_port["vlan"], switch_port["voiceVlan"], switch_port["allowedVlans"], switch_port["poeEnabled"], "", switch_port["rstpEnabled"], switch_port["stpGuard"], "")