Python: Bulk rename Juniper switches

Python: Bulk rename Juniper switches

Juniper/Mist devices can be easily claimed via the web UI and/or the Mist AI app on the mobile device. It has been a while since Juniper also added support to claim switches and I just finally had a chance to test it out. For some reason my phone was not able to scan the QR code from the box, but it was able to successfully scan it from the switch.

Mist AI – Unassigned Switches

This process will add the switch to your Org inventory. Switch still needs to be assigned to a site and it’s name configured after this step. Few points to note here:

  • Org Switch Template and/or Site Switch Templates can be configured in advance. This way once a switch or multiple switches get assigned, they will automatically download the configuration.
  • These templates can be configured via APIs
  • Org level switch template can be exported and then imported from the same org or a different org as a .json file. (NOTE: This is what I did in my scenario. I simply exported an already configured template from one one Org into my Org.
  • Site level switch template can not be exported, at least currently.

Back to the switch configuration. Now I have my templates configured and my script ready, I can assign the switch or switches to the site via web UI or Mist AI app. Now I want to rename all the switches. Renaming one or two switches is not a big deal. Renaming 10+ switches can become time consuming.

First step would be to get the switch ID. I utilized a python script below to get the device ID.

import requests
url = 'https://api.mist.com/api/v1/orgs/:org-id/inventory'

headers = {
	'Content-Type': 'application/json',
	'Authorization': 'Token abcdefg12345678910'
}

results = requests.get(url, headers=headers)
inventory = results.json()
for inventory in inventory:
	print(inventory['name'], ':',inventory['mac'], ':',inventory['serial'], ':', inventory['id'])
Mist – Org Inventory

Top most device is my switch that hasn’t been assigned and does not have a name yet. I copied the ID and added it to the csv file. NOTE: This can also be accomplished by simply exporting the output to the csv file also.

switches.csv file

Now all that is needed is to name the switches in Column B and then running the script to bulk rename them:

import requests
import csv

headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Token abcdefg12345678'
}

f = open('switches.csv')
csv_f = csv.reader(f)
next(csv_f)

for Device_ID,name in csv_f:
 print (Device_ID,name)
 base_url = 'https://api.mist.com/api/v1/sites/:site-id/devices/'
 url = base_url + Device_ID
 print(url)
 payload = "{\n  \"name\": \"%s\"\n}" % (name)
 print(payload)
 response = requests.request("PUT", url, headers=headers, data=payload)
 print(response.text.encode('utf8'))

After running the script, switch name was changed successfully.

Python – Switch renamed

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WordPress SEO