Python Script for Updating Vlans subnets on 1400+ networks

cionica1993
Here to help

Python Script for Updating Vlans subnets on 1400+ networks

Hello all,

 

First of all I'm a newbie in python and I've been trying to use the python wrappers from https://github.com/meraki/dashboard-api-python in order build a script to update 1400+ vlans. I've been failing since I started.

Can anyone help me with this ? At least provide some guidance.

I would also need to parse a .csv file in order to map the correct subnet to the NetworkID. 

9 Replies 9
boundless-digit
Getting noticed

Hi,

Can you paste your code and show where you are getting blocked?
Sidney Burks
Captive Portal and Meraki API Automation
Founder and CTO, Boundless Digital
sidney@boundless.fr
https://www.boundless.fr
RubenG
Getting noticed

Can you give us more information? What aspects are you trying to update or generate on the VLANs?

Are you changing your subnets and IP addressing, Migrating from a class A to a Class C network. etc, changing the VLAN, Generating new VLANs, more than one VLAN per Network or just one, etc.?

I would start by pulling all the network ID's, putting it in a CSV file. Then update the CSV file with the subnet that each NetworkID is getting.
After that, you can import to Python using the CSV to List module. Sort through all of them and push it out through the API calls.
You can then doublecheck your work by GETing the subnets after you updated them and comparing them to your master list.

Let me know if this helps.
cionica1993
Here to help

My network is full of MX's. I'm just updating the default VLAN 1 subnet, I had to use a configuration template and a bulk network template. In the configuration template a assigned a /15 from which each network will be randomly assigned a /26 ( that's how the template works ). After that I deployed a bulk template with all the locations and applied the configuration template on them.

I pulled all the organizations information, it was pulled in a JSON file. I turned that into a .CSV and created one column with the network ID, another with the network and subnet and another with the MX IP.

I started today to watch again a python course from Udemy, hopefully this week I'll be able to write the code.

I found the Update networks vlan python wrapper from https://github.com/meraki/dashboard-api-python and now I need to write the code for parsing the CSV and assign those variables into the wrapper.
PhilipDAth
Kind of a big deal
Kind of a big deal

Are you trying to update VLANs on an MX or an MS?  I'm guessing you are trying to create layer 3 interfaces and somehow map some ports to that VLAN?

 

Break the problem down into smaller steps.  As you get each step done move onto the next.

 

Perhaps try retrieving the information first for practice.

cionica1993
Here to help

VLAN's on MX, the whole network consists of MX's. I just need to update the default VLAN's subnets, basically the LAN network ( i'm not using multiple vlans)

It's really a simple network, just really big.
RubenG
Getting noticed

Check this out, you can also use Google Docs:
https://developer.cisco.com/meraki/build/google-forms-with-the-dashboard-api/

Now that you have the CSV file you can make the API call from your script. Loop through all of the Network IDs and change each networks address on each iteration.
cionica1993
Here to help

Thanks a lot ! I'll post the code when it's done. However, I think I stumbled on another problem. Given the fact that the IP's have been assigned randomly through the template, if I have an IP subnet which is already used and I try to update it on the right location the dashboard will not let me.

So right now, I'm stuck in thinking how to solve this first.
PhilipDAth
Kind of a big deal
Kind of a big deal

Open a ticket with support and ask them to disable the "strict" IP address check for AutoVPN.

 

This will allow you to configure sites with the same IP address, which will break things.  However by the time your script finishes the addressing should be unique again.

 

Then ask support to turn the strict option back on again.

 

cionica1993
Here to help

Hello,

So I think I've done it. I tested it on 3 networks and it worked. See the code below:
import csv
from datetime import datetime
import os

import meraki

# Either input your API key below by uncommenting line 10 and changing line 15 to api_key=api_key,
# or set an environment variable (preferred) to define your API key. The former is insecure and not recommended.
# For example, in Linux/macOS: export MERAKI_DASHBOARD_API_KEY=093b24e85df15a3e66f1fc359f4c48493eaa1b73
api_key = 'xxx'


def updateNetworkVlan(self, networkId: str, vlanId: str, **kwargs):

csv_file = open('TestScript.csv')
csv_reader = csv.reader(csv_file, delimiter=',')
# csv_reader stocheaza id retea si subnet
print(csv_reader)
for NetworkID, Subnet, IP in csv_reader:
print(NetworkID, Subnet, IP)

collect = {}
network_id = NetworkID
collect[NetworkID] = network_id

vlan_id = '1'
collect['vlan_id'] = vlan_id

update_network_vlan = UpdateNetworkVlanModel()
update_network_vlan.name = 'Agentii'
update_network_vlan.subnet = Subnet
update_network_vlan.appliance_ip = IP
update_network_vlan.dhcp_handling = DhcpHandlingEnum.ENUM_RUN_A_DHCP_SERVER
update_network_vlan.dhcp_lease_time = DhcpLeaseTimeEnum.ENUM_1_DAY
update_network_vlan.dhcp_boot_options_enabled = False
update_network_vlan.dhcp_boot_next_server = 'null'
update_network_vlan.dhcp_boot_filename = 'null'
update_network_vlan.dns_nameservers = 'google_dns'
collect['update_network_vlan'] = update_network_vlan

result = vlans_controller.update_network_vlan(collect)


One more question tough, after I run it I would like to get a print back that everythong was OK or if I got any errors on any networks. How can I do that?

Really appreciate the guidance !!
Get notified when there are additional replies to this discussion.