API scripting can be a very powerful method of making large scale changes or pulling information.
I needed to rename all of the devices in my enterprise using a new standard hostname convention. With hundreds of devices under my care, how can I do this without spending countless hours on mundane work?
In this article, I will show you how to pull a device inventory using the Meraki API and change the hostnames for all your devices.
First, login into your Meraki dashboard. Go to Organization, then Settings. Check the box to "enable access to the Cisco Meraki Dashboard API". Save the changes and then click on the profile hyperlink.
Inside your profile screen click the box to "Generate new API Key". Copy this key down on a notepad.
Using our Linux server create a variable with the API key value using the following command and your apikey value:
export apikey=41aa26b8fasdfasdfadsfasdfasdfasdfda45f408
Now we need to find our Company Organization ID with the following command:
curl --location --request GET "https://api.meraki.com/api/v1/organizations" \
--header "X-Cisco-Meraki-API-Key: $apikey" | jq .
(jq is a JSON processor that will need to be installed separately)
Now we are ready to create a second variable for Org ID:
export organizationId=6asdf24
We can now run commands like the following to do a dump of all devices:
curl --location --request GET "https://api.meraki.com/api/v1/organizations/$organizationId/devices/statuses" \
--header "X-Cisco-Meraki-API-Key: $apikey" | jq .
This is great information but not really useable...
We can massage this information into a more usable format by using jq to build a csv file using only the variables we want. In this case, I was interested mainly in the following variables.
jq ".[] | [.name, .serial] | @csv"
We can combine this jq command with our Curl command, then send it through some "sed" and "tr" filters to strip out the unnecessary " and \ characters. Then send the data to a file using > filename.csv
sed -e 's/"//g' | tr -d '\\'
Final command:
curl --location --request GET "https://api.meraki.com/api/v1/organizations/$organizationId/inventoryDevices" \
--header "X-Cisco-Meraki-API-Key: $apikey" | jq ".[] | [.name, .serial] | @csv" | sed -e 's/"//g' | tr -d '\\' > merakiinventory.csv
I am going to open this file with excel and add a new row for the column titles and a new column for New Name and Command.
The command we want to run is "[PUT] https://api.meraki.com/api/v0/networks/network_id/sm/device/fields?serial={{serial}}&deviceFields={"name":"{{name}}"}"
So to get this into the right format I am going to use the "=concatenate()" function in excel to create a new command using some text, our known variables, and some of our spreadsheet variables.
=CONCATENATE("curl -L -H 'X-Cisco-Meraki-API-Key: 7b358asdfasdfasdfasdfasdfasdf58797bd' -H 'Content-Type: application/json' -X PUT --data-binary '{""name"":""",C2"""}' 'https://api.meraki.com/api/v1/devices/",B2,"'")
Now that we have the commands to change our hostnames completely written out, we can implement the script in a couple of different ways... Copy and paste a chunk at a time or create a bash script to execute all of the commands for us. I am going to do the later to show a more automated route to success. Time to SSH back to the linux network management server.
First, create a file using your preferred text editor.
nano changename.bash
BASH files all begin with the following command. Use "set -x" to turn echo on to see your commands progress in real time.
#!/bin/bash
set -x #echo on
Then paste in the commands from the excel file column D.
Make the file executable with "chmod +x changename.bash" and fire away using "./changename.bash"
-Brett
Reference:
https://documentation.meraki.com/zGeneral_Administration/Other_Topics/The_Cisco_Meraki_Dashboard_API
Comments