INFO: get ALL SM devices in ALL networks for a given org ID and API key

PaulF
Meraki Employee
Meraki Employee

INFO: get ALL SM devices in ALL networks for a given org ID and API key

Hi,

 

I had a request from a customer who, because they had 35 networks, wanted a more automated way to export Systems Manager device data. So, I wanted to share this:

 

# This script, for a given API key, and org ID
# For each network, get the SM devices and print to screen
# Mandatory arguments:
# -k <API KEY>      : Your Meraki Dashboard API Key
# -o <Org. ID>      : Your Meraki Org ID
# Pre requisites:
# Meraki library : pip install meraki : https://developer.cisco.com/meraki/api/#/python/getting-started

import meraki
import sys, getopt


def main(argv):

    print("Meraki Library version: ")
    print(meraki.__version__)

    arg_apikey = False

    try:
        opts, args = getopt.getopt(argv, 'k:o:')
    except getopt.GetOptError:
        printhelp(argv)
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-k':
            arg_apikey = arg
        if opt == "-o":
            arg_orgId = arg

    # Create Meraki Client Object and initialise
    client = meraki.DashboardAPI(api_key=arg_apikey)

    networks = client.organizations.getOrganizationNetworks(organizationId=arg_orgId)

    print("networkID, model, osName, systemModel")
    for network in networks:
        networkName = (network['name'])
        networkID = (network['id'])

        try:
            devices = client.sm.getNetworkSmDevices(networkId=networkID, fields={"ip"})
            if len(devices) == 0:
                # no devices
                print("No devices in this SM network")
            else:
                for device in devices:
                    print(networkID, ",", device["name"], ",", device["osName"], ",", device["systemModel"])

        except meraki.APIError as e:
            print("No SM in " + networkName + " network")



def printhelp():
    # prints help information
    print('This is a script to Get the SM ldevices across multiple networks.')
    print('')
    print('Mandatory arguments:')
    print(' -k <api key>         : Your Meraki Dashboard API key')
    print(' -o <Org. ID>         : Your Meraki Org ID')



if __name__ == '__main__':
    main(sys.argv[1:])

 

You'll note:

fields={"ip"} : As per https://developer.cisco.com/meraki/api-latest/#!get-network-sm-devices 

The default fields are: id, name, tags, ssid, wifiMac, osName, systemModel, uuid, and serialNumber. The additional fields are: ip,
systemType, availableDeviceCapacity, kioskAppName, biosVersion, lastConnected, missingAppsCount, userSuppliedAddress, location, lastUser, ownerEmail, ownerUsername, osBuild, publicIp, phoneNumber, diskInfoJson, deviceCapacity, isManaged, hadMdm, isSupervised, meid, imei, iccid, simCarrierNetwork, cellularDataUsed, isHotspotEnabled, createdAt, batteryEstCharge, quarantined, avName, avRunning, asName, fwName, isRooted, loginRequired, screenLockEnabled, screenLockDelay, autoLoginDisabled, autoTags, hasMdm, hasDesktopAgent, diskEncryptionEnabled,hardwareEncryptionCaps, passCodeLock, usesHardwareKeystore, androidSecurityPatchVersion, and url.

 

print(networkID, ",", device["name"], ",", device["osName"], ",", device["systemModel"])

the script prints just a handful of fields returned for demo purposes. You can expand the above as you see fit

 

It's called with:

 

python3 yourscriptname.py -k yourMerakiAPIKey -o yourMerfakiOrgID

 

1 Reply 1
PhilipDAth
Kind of a big deal
Kind of a big deal

Good effort!

Get notified when there are additional replies to this discussion.