Powerful reports with device42 using the self describing APIs

Device42’s comprehensive data center management solution provides REST APIs that are self-describing and you can use these to get retrieve and update data. In the following example, I will show how you can use a simple python script to create a report you need. We will be creating a CSV file for switchports with switchport name, switch names and what devices are connected to it.

Self describing APIs


Device42 APIs are documented at:
These are accessible from your browser as well and give you an idea on what to expect from a get call for an object. Above image shows what you can see for switchport API. We will create report with just 3 fields as highlighted in the image above.

Overview of the python script

Essence of the script is simple;
1. Call device42 API to grab the switchport values.
2. iterate through the json values received via the API, and
3. Create the CSV file, that serves as our report.

Let us go over these sections one by one.

GET call for device42 API

d42_get_switchports_url = D42_URL+'/api/1.0/switchports/'
request = urllib2.Request(d42_get_switchports_url)
request.add_header('Authorization', 'Basic ' + b64encode(D42_USERNAME + ':' + D42_PASSWORD))
    r = urllib2.urlopen(request)
    obj =
    switchportdata = json.loads(obj)

Iterate over the values and create the CSV file

    f = csv.writer(open("test.csv", "wb+"))
    for key,value in  switchportdata.iteritems():
        for i in value:
            f.writerow([i['switch']['name'], i['port'], i['devices']])

Complete script

The full script is available for download at device42 github repository Misc_D42_Python_Scripts

Share this post

About the author