All Client Pull via Python

MigzBernadas
New here

All Client Pull via Python

Hi guys!

Good day!

 

I'm having trouble getting the "OS" and "manufacturer" of all the devices in our organization every time I use the code below.

Let me know if I need to remove or add something up.

Thanks!

 

Here's the screenshot of the error

MigzBernadas_0-1596117983211.png

 

Code below:

 

# Import "meraki" to allow Meraki function calls
import meraki

# Import "datetime" to get current date and time
import datetime

# ///////// Start Definition of Script Constants ////////////////
# api_key = Unique API key generated on a per Meraki User basis
# org_id = Unique organization ID of organization you want to query

api_key = ''
org_id = ''


# Get the current list of networks from your Organization
current_networks = meraki.getnetworklist(api_key, org_id)


# Establish when "now" is, used to create the file name for output
now = datetime.datetime.now()

# Create/Name the output file with year, month, day and hour minutes
filename = now.strftime("%Y-%m-%d %H.%M") + ".csv"

# Open the file for output
f = open(filename,"w+")

# "i" will be used to increment through each "network" in the Organization
i = 0

for i in range(len(current_networks)):

# Output to console the Name and ID of the current network
print("Network Name:" + current_networks[i]['name'])
print("Network ID:" + current_networks[i]['id'])

curr_net_id = current_networks[i]['id']
curr_net_devices = meraki.getnetworkdevices(api_key,curr_net_id)

# Write to the output file the 'name' of the network and add a comma
f.write(current_networks[i]['name'])
f.write(",")

# Checks for Meraki devices on this network, if there are no devices output to file "No Devices Present"
if len(curr_net_devices) == 0:
f.write("No devices Present")
f.write("\n")

# "j" will be used to increment through each "device" on this network
j = 0

for j in range(len(curr_net_devices)):
# Output to console the serial number of the device
print ("Device Serial:" +curr_net_devices[j]['serial'])
curr_clients = meraki.getclients(api_key,curr_net_devices[j]['serial'])


# Checks for clients attached to this device, if no clients present output the name, model and serial of the
# device as well as the message "No Clients". Also catch the error that comes up in case this is a device for
# which clients are not relevant, e.g. MV camera's.
if len(curr_clients) == 0 or curr_clients[0] == "Invalid device type":
# Output to console "No Clients" message
print ("No clients")

# Output to file "name", "model", "serial" of the device as well as "No Clients" message
f.write(current_networks[i]['name'])
f.write(',')
f.write(curr_net_devices[j]['model'])
f.write(',')
f.write(curr_net_devices[j]['serial'])
f.write(",")
f.write("No Clients")
f.write("\n")

# Only list the clients if there are any.
else:
# "k" will be used to increment the number of clients on each device
k = 0

for k in range(len(curr_clients)):
# Output to console "ip" and "mac" of the client
print(curr_clients[k]['ip'],' ',curr_clients[k]['mac'],' ',curr_clients[k]['description'],' ',curr_clients[k]['vlan'],curr_clients[k]['os'])
# Output to file "name", "model", "serial" of the device as well as the "ip" and "mac"
# of the client
f.write(str(current_networks[i]['name']))
f.write(',')
f.write(str(curr_net_devices[j]['model']))
f.write(',')
f.write(str(curr_net_devices[j]['serial']))
f.write(',')
f.write(str(curr_clients[k]['description']))
f.write(',')
f.write(str(curr_clients[k]['ip']))
f.write(',')
f.write(str(curr_clients[k]['mac']))
f.write(',')
f.write(str(curr_clients[k]['vlan']))
f.write(',')
f.write(str(curr_clients[k]['os']))
f.write("\n")

# Increment "k" to move onto next client on current device
k = k + 1

# Increment "j" to move onto next device on current Network
j = j + 1
# Increment "i" to move onto next network in Organization
i = i + 1

# Close the output file
f.close()

 

 

 

 

 

1 Reply 1
Nash
Kind of a big deal

Key error means the key doesn't necessarily exist.

 

I'd recommend adding a variable with your dict.keys() so you're not running dict.keys() repeatedly, then checking if the key exists before you do something with it.

 

keys = curr_clients[k].keys()

if 'os' in keys:
    # Do something with curr_clients[k]['os'] here

 

Or you could do something like a try/except block, but I usually end up doing the "if 'key name' in keys" thing for Reasons.

 

Get notified when there are additional replies to this discussion.