Last active
April 20, 2018 10:26
-
-
Save dexterlabora/abd40a02776812f7ce65ebefed734fe0 to your computer and use it in GitHub Desktop.
List Meraki clients in a network by traversing each network device.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Meraki Clients on a Network | |
Traverse the Meraki Dashboard API to display the Clients and Group Policies for each device in a network. | |
npm install axios json-bigint --save | |
Run: | |
$ API_KEY=2f30UseYourOwnAPIkEyd170f NET=L_12345678 node listClients-Network.js | |
Basic Flow using async/await | |
- get devices (netId) | |
- for each device (serial), get clients | |
- for each client, get group policies | |
*/ | |
const axios = require("axios"); | |
var apiKey = process.env.API_KEY || ''; // config env params | |
var netId = process.env.NET || ''; | |
var baseUrl = 'https://api.meraki.com/api/v0'; | |
const meraki = axios.create({ | |
baseURL: baseUrl, | |
headers: { 'X-Cisco-Meraki-API-Key': apiKey } | |
}); | |
const summary = async () => { | |
console.log("Running Meraki Client Summary Tool..."); | |
try { | |
// GET Devices | |
let devices = [] = await meraki.get('/networks/'+netId+'/devices').then((res) => {return res.data}); | |
//console.log("Devices: ", devices); | |
// GET Clients for each Device | |
for (let d of devices){ | |
console.log("\n-- Device -- \n Name: "+d.name +"\n Serial: "+d.serial+"\n Model: "+d.model); | |
try { | |
let clients = [] = await meraki.get('/devices/'+d.serial+'/clients?timespan=84000').then((res) => {return res.data}); | |
//console.log("Clients:", clients); | |
console.log("\n-- Clients --"); | |
// GET Policies for each Client | |
for (let c of clients){ | |
console.log("\n Client Name: "+c.dhcpHostname +"\n MAC: "+c.mac); | |
try { | |
let policies = await meraki.get('/networks/'+netId+'/clients/'+c.mac+'/policy?timespan=84000').then((res) => {return res.data}); | |
//console.log("Policies: ", policies); | |
console.log(" - Policy Type "+ policies.type); | |
if(policies.groupPolicyId){ | |
console.log(" - ID: "+policies.groupPolicyId); | |
} | |
} catch (e){ | |
console.log("error in policies: ", e); | |
} | |
} | |
} catch (e) { | |
console.log("error in clients: ", e.response.data.errors[0]); | |
} | |
} | |
console.log("\n \n Done! \n"); | |
} catch (e) { | |
console.log("error in devices: ", e); | |
} | |
} | |
summary(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment