示例#1
0
def replaceForm():
    form = ReplaceDevice()
    if form.validate_on_submit():
        message = []

        postNetwork = form.networkField.data
        netname = merakiapi.getnetworkdetail(apikey, postNetwork)
        oldMX = form.oldMX.data
        newMX = form.newMX.data
        oldSwitch = form.oldSwitch.data
        newSwitch = form.newSwitch.data
        oldAP = form.oldAP.data
        newAP = form.newAP.data

        if oldMX is not '':
            oldconfig = merakiapi.getdevicedetail(apikey, postNetwork, oldMX)
            merakiapi.updatedevice(apikey,
                                   postNetwork,
                                   newMX,
                                   name=oldconfig['name'],
                                   tags=oldconfig['tags'],
                                   lat=oldconfig['lat'],
                                   lng=oldconfig['lng'],
                                   address=oldconfig['address'],
                                   move='true')
            result = merakiapi.removedevfromnet(apikey, postNetwork, oldMX)
            if result == None:
                message = Markup(
                    'MX with serial <strong>{}</strong> successfully deleted from Network: <strong>{}</strong>'
                    .format(oldMX, netname['name']))
            merakiapi.claim(apikey, organizationid, serial=newMX)
            result = merakiapi.adddevtonet(apikey, postNetwork, newMX)
            if result == None:
                message = Markup(
                    'MX with serial <strong>{}</strong> successfully added to Network: <strong>{}</strong>'
                    .format(newMX, netname['name']))

        if oldSwitch is not '':
            #ADD NEW SWITCH TO NETWORK
            merakiapi.claim(apikey, organizationid, serial=newSwitch)
            result = merakiapi.adddevtonet(apikey, postNetwork, newSwitch)
            oldconfig = merakiapi.getdevicedetail(apikey, postNetwork,
                                                  oldSwitch)
            merakiapi.updatedevice(apikey,
                                   postNetwork,
                                   newSwitch,
                                   name=oldconfig['name'],
                                   tags=oldconfig['tags'],
                                   lat=oldconfig['lat'],
                                   lng=oldconfig['lng'],
                                   address=oldconfig['address'],
                                   move='true')
            if result == None:
                message = Markup(
                    'Switch with serial <strong>{}</strong> successfully added to Network: <strong>{}</strong>'
                    .format(newSwitch, netname['name']))
                #CLONE L2 PORT CONFIGS
                if '24' in oldconfig['model']:
                    numports = 30
                elif '48' in oldconfig['model']:
                    numports = 54
                elif '16' in oldconfig['model']:
                    numports = 22
                elif '32' in oldconfig['model']:
                    numports = 38
                for port in range(1, numports):
                    config = merakiapi.getswitchportdetail(
                        apikey, oldSwitch, port)
                    print(config)
                    # Clone corresponding new switch
                    # Tags needed to be input as a list
                    #if config['tags'] is not '':
                    #    tags = config['tags'].split()
                    #else:
                    tags = []

                    # Access type port
                    if config['type'] == 'access':
                        merakiapi.updateswitchport(
                            apikey,
                            newSwitch,
                            port,
                            name=config['name'],
                            tags=tags,
                            enabled=config['enabled'],
                            porttype=config['type'],
                            vlan=config['vlan'],
                            voicevlan=config['voiceVlan'],
                            poe='true',
                            isolation=config['isolationEnabled'],
                            rstp=config['rstpEnabled'],
                            stpguard=config['stpGuard'],
                            accesspolicynum=config['accessPolicyNumber'])

# Trunk type port
                    elif config['type'] == 'trunk':
                        merakiapi.updateswitchport(
                            apikey,
                            newSwitch,
                            port,
                            name=config['name'],
                            tags=tags,
                            enabled=config['enabled'],
                            porttype=config['type'],
                            vlan=config['vlan'],
                            allowedvlans=config['allowedVlans'],
                            poe='true',
                            isolation=config['isolationEnabled'],
                            rstp=config['rstpEnabled'],
                            stpguard=config['stpGuard'])
            #404 MESSAGE FOR INVALID SERIAL IS BLANK, POPULATE ERROR MESSAGE MANUALLY
            elif result == 'noserial':
                message = Markup(
                    'Invalid serial <strong>{}</strong>'.format(serial))
            else:
                message = result
            #REMOVE OLD SWITCH FROM NETWORK
            merakiapi.removedevfromnet(apikey, postNetwork, oldSwitch)

        if oldAP is not '':
            oldconfig = merakiapi.getdevicedetail(apikey, postNetwork, oldAP)
            merakiapi.updatedevice(apikey,
                                   postNetwork,
                                   newAP,
                                   name=oldconfig['name'],
                                   tags=oldconfig['tags'],
                                   lat=oldconfig['lat'],
                                   lng=oldconfig['lng'],
                                   address=oldconfig['address'],
                                   move='true')
            result = merakiapi.removedevfromnet(apikey, postNetwork, oldAP)
            if result == None:
                message = Markup(
                    'AP with serial <strong>{}</strong> successfully deleted from Network: <strong>{}</strong>'
                    .format(oldMX, netname['name']))
            merakiapi.claim(apikey, organizationid, serial=newAP)
            result = merakiapi.adddevtonet(apikey, postNetwork, newAP)
            if result == None:
                message = Markup(
                    'AP with serial <strong>{}</strong> successfully added to Network: <strong>{}</strong>'
                    .format(newMX, netname['name']))

        #SEND MESSAGE TO SUBMIT PAGE
        flash(message)
        return redirect('/submit')
    return render_template('replace.html',
                           title='Meraki Device Provisioning',
                           form=form)
print (device_id_list)

for dev in device_id_list:
	switch_clients = merakiapi.getclients(api_key,dev)
	#print (switch_clients)
	for c in switch_clients:
		for key,value in c.items():
			if key == 'switchport':
				c_port = str(value)
			elif key == 'ip':
				c_ip = str(value)
			elif key == 'vlan':
				c_vlan = str(value)
			elif key == 'dhcpHostname':
				c_name = str(value)
			else:
				continue
		if meraki_info.ip_address_string in c_ip and c_vlan == meraki_info.source_vlan:
			if c_port == meraki_info.uplink1 or c_port == meraki_info.uplink2: # SPECIFY UPLINK PORTS TO PROTECT
				print ("UPLINK PORT %s FOR CLIENT %s, SKIPPING..." % (c_port, c_name))
			else:
				print ("MOVING CLIENT %s TO VLAN %s ON SWITCH %s PORT %s" % (c_name, meraki_info.dest_vlan, dev, c_port)) 
				swport = merakiapi.getswitchportdetail(api_key, dev, c_port)
				print (swport)
				swport['vlan'] = meraki_info.dest_vlan
				#print (swport)
				print(merakiapi.updateswitchport (api_key, dev, swport['number'], swport['name'], swport['tags'], True, porttype=swport['type'], vlan=swport['vlan'], voicevlan=swport['voiceVlan'], allowedvlans=swport['allowedVlans'], poe=True, isolation=swport['isolationEnabled'], rstp=swport['rstpEnabled'], stpguard=swport['stpGuard'], accesspolicynum=swport['accessPolicyNumber']))
				swport = merakiapi.getswitchportdetail(api_key, dev, c_port)
				#print (swport)