IMPORTANT: THE CODE BELOW IS MESSED UP IF YOU COPY IT TO NOTEPAD AND TRY TO RUN IT BECAUSE OF THE WAY IT WAS FORMATTED ON THE FORUM. IF ANYONE KNOWS OF A GOOD WAY TO PASTE IN MY CODE PLEASE ADVISE.
First off, you should know that I'm not a programmer. I was stumbling most of the way when I created this simple bash script. I'm sure when you look at the script you will see some inefficiencies with the way I chose to do certain things. Believe me, I would love to make it better, but this is what I'm sharing with you. I don't take any responsibility for this code or my methodologies.
So this is the goal. Update multiple networks with the same content filtering blocked categories.
I accomplished this using a simple CSV file, writing a bash script using the Meraki-CLI, and iterating through each network in the CSV.
NOTE: When I first started this task, I was well aware that Python would be the superior choice, but I decided to try the Meraki-CLI
Step 1: https://meraki-cli.readthedocs.io/en/latest/step-by-step_installation/ this assumes you are using Windows like I am. Follow the step by step guide on installing Meraki-CLI.
Step 2: Launch Ubuntu on Windows. Enable Quick Edit Mode on your Ubuntu instance. This will make life easier when you are copying the output from WSL to your CSV file.
Step 3: Make sure you have your Meraki dashboard API key. If you don't know what this is, there are plenty of youtube videos on how to find it. Follow this guide once you have it. Use the config file method - https://meraki-cli.readthedocs.io/en/latest/getting-your-api-key/
Step 4: Follow this guide to get your organization ID https://meraki-cli.readthedocs.io/en/latest/a-few-starting-commands/
Step 5: Get a listing of all your networks and copy the network IDs and paste into your CSV file. Edit the CSV file to include only the networks you wish to modify. Use following command to get the network IDs. meraki organizations getOrganizationNetworks --organizationId XXX
The CSV file that I use looks like this, it just has two column. On column A I have names and on column B I have the network IDs. Use Excel to create the CSV file. Save it as a CSV to the directory you create in step 6.
Step 6: create a directory on Ubuntu in your home directory to store all the files for this task. The script will go here, CSV, and a few other files that are created when the script is executed.
Since you most likely created the CSV using Windows, Open file Explorer and goto \\wsl$, you will most likely see the Ubuntu instance. Navigate to the folder you just created on Ubuntu and drop the CSV in there.
NOTE: DO NOT RUN THE CODE BELOW UNTIL YOU HAVE EDITED ALL THE CATEGORIES IN THE SCRIPT TO YOUR LIKING. THE CODE BELOW IS JUST AN EXAMPLE. YOU CAN FIND THE CATEGORIES BY FOLLOWING STEP 7
Step 7: Run the following command using a NetworkID that you collected in step 5 to get all the categories listed out. Note that all your MX firmware versions should be updated to the same version for best results.
meraki appliance getNetworkApplianceContentFilteringCategories --networkId XX
Use the category ID's to edit the script below to your liking.
Step 8: Create a bash script using the below code. Update the file variable to reflect the new directory you created and CSV. Drop the bash file into your ubuntu directory using file explorer if you are using Windows to create the bash script.
Step 9. Run the code in your newly created directory on Ubuntu: sh XX.sh is the name of your script.
#!/bin/bash
#The file variable points to the csv file used for the networkIDs. The CSV file can be updated as needed and the script re-ran to keep everything the same throughout the organization.
file="/home/xxx/meraki_scripts/CommunityNetworkIDListing.csv"
#The IFS variable is used to identify delimiters in the CSV file. The OLDIFS variable contains the state of IFS before we redefine IFS.
OLDIFS=$IFS
#Very basic cleanup of the temp files used in this script. The script must be ran in the ../meraki_scripts/ subfolder, otherwise these files be cleaned up.
rm networkID.txt
rm networkID2.txt
#The while loop parses the CSV and echos only the networkID field to a text file. The text files have carriage returns #as a result of this while loop. I could not figure out how to do this script in the entire while loop because when I would try to pass the network variable to the meraki cli i would get errors, most likely related to some carriage return. So I opted to just echo it to a text file. Then I removed all the carriage returns from the text file which you will see in the below sed command.
while IFS="," read comm network
do
echo "$network" >> networkID.txt
done < $file
#Returns the default IFS state
IFS=$OLDIFS
#The sed command removes the carriage returns and outputs it to a new file.
sed 's/\r$//' networkID.txt > networkID2.txt
communities=$(cat networkID2.txt)
#main meraki cli command that sets the content filtering categories for all communities.
for community in $communities
do
meraki appliance updateNetworkApplianceContentFiltering --networkId $community --blockedUrlCategories '
["meraki:contentFiltering/category/C50",
"meraki:contentFiltering/category/C84",
"meraki:contentFiltering/category/C92",
"meraki:contentFiltering/category/C112",
"meraki:contentFiltering/category/C122",
"meraki:contentFiltering/category/C25",
"meraki:contentFiltering/category/C64",
"meraki:contentFiltering/category/C119",
"meraki:contentFiltering/category/T01",
"meraki:contentFiltering/category/T02",
"meraki:contentFiltering/category/T03",
"meraki:contentFiltering/category/T04",
"meraki:contentFiltering/category/T05",
"meraki:contentFiltering/category/T16",
"meraki:contentFiltering/category/T06",
"meraki:contentFiltering/category/T13",
"meraki:contentFiltering/category/T12",
"meraki:contentFiltering/category/T21",
"meraki:contentFiltering/category/T23",
"meraki:contentFiltering/category/T08",
"meraki:contentFiltering/category/T09",
"meraki:contentFiltering/category/T11",
"meraki:contentFiltering/category/T10",
"meraki:contentFiltering/category/T07"]'
done