Hello Forum
I have written the following code to output Networks and switches for my organisation
import csv
import meraki
import requests
# Replace with your Meraki API Key
API_KEY = 'MYORGAPIKEY'
# Replace with your organization ID
ORG_ID = 'MYORGID'
# Initialize the Meraki dashboard API client
dashboard = meraki.DashboardAPI(API_KEY, output_log=False, suppress_logging=True)
# Fetch a list of all networks in the organization
networks = dashboard.organizations.getOrganizationNetworks(ORG_ID)
# Prepare CSV output
with open('merakiNetworksAndSwitches.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['NetworkID','NetworkName', 'DeviceName', 'Serial', 'Model', 'MAC','IP'])
# Iterate through networks
for network in networks:
NetworkID = network['id']
NetworkName = network['name']
# Get devices in each network
devices = dashboard.networks.getNetworkDevices(NetworkID)
for device in devices:
DeviceName = device.get('name', 'N/A')
Serial = device.get('serial')
Model = device.get('model', 'N/A')
MAC = device.get('mac', 'N/A')
IP = device.get("lanIp", 'N/A')
writer.writerow({
NetworkID,
NetworkName,
DeviceName,
Serial,
Model,
MAC,
IP
})
print(f"Network And Switches Data Saved To merakiNetworksAndSwitches.csv")
However when I run this it comes up with the error
Traceback (most recent call last):
File "<stdin>", line 14, in <module>
ValueError: I/O operation on closed file.
>>> print(f"Network And Switches Data Saved To merakiNetworksAndSwitches.csv")
Can someone please help?
Solved! Go to solution.
Perhaps you have an indentation error as your code works for me, only thing I changed was the curly braces {} to square ones [] when you are doing the writer.writerow within the devices for loop.
import csv
import meraki
import requests
# Replace with your Meraki API Key
API_KEY = 'dontlookatmykey'
# Replace with your organization ID
ORG_ID = 'xxxx'
# Initialize the Meraki dashboard API client
dashboard = meraki.DashboardAPI(API_KEY, output_log=False, suppress_logging=True)
# Fetch a list of all networks in the organization
networks = dashboard.organizations.getOrganizationNetworks(ORG_ID)
# Prepare CSV output
with open('merakiNetworksAndSwitches.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['NetworkID','NetworkName', 'DeviceName', 'Serial', 'Model', 'MAC','IP'])
# Iterate through networks
for network in networks:
NetworkID = network['id']
NetworkName = network['name']
# Get devices in each network
devices = dashboard.networks.getNetworkDevices(NetworkID)
for device in devices:
DeviceName = device.get('name', 'N/A')
Serial = device.get('serial')
Model = device.get('model', 'N/A')
MAC = device.get('mac', 'N/A')
IP = device.get("lanIp", 'N/A')
writer.writerow([
NetworkID,
NetworkName,
DeviceName,
Serial,
Model,
MAC,
IP
])
print(f"Network And Switches Data Saved To merakiNetworksAndSwitches.csv")
Perhaps you have an indentation error as your code works for me, only thing I changed was the curly braces {} to square ones [] when you are doing the writer.writerow within the devices for loop.
import csv
import meraki
import requests
# Replace with your Meraki API Key
API_KEY = 'dontlookatmykey'
# Replace with your organization ID
ORG_ID = 'xxxx'
# Initialize the Meraki dashboard API client
dashboard = meraki.DashboardAPI(API_KEY, output_log=False, suppress_logging=True)
# Fetch a list of all networks in the organization
networks = dashboard.organizations.getOrganizationNetworks(ORG_ID)
# Prepare CSV output
with open('merakiNetworksAndSwitches.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['NetworkID','NetworkName', 'DeviceName', 'Serial', 'Model', 'MAC','IP'])
# Iterate through networks
for network in networks:
NetworkID = network['id']
NetworkName = network['name']
# Get devices in each network
devices = dashboard.networks.getNetworkDevices(NetworkID)
for device in devices:
DeviceName = device.get('name', 'N/A')
Serial = device.get('serial')
Model = device.get('model', 'N/A')
MAC = device.get('mac', 'N/A')
IP = device.get("lanIp", 'N/A')
writer.writerow([
NetworkID,
NetworkName,
DeviceName,
Serial,
Model,
MAC,
IP
])
print(f"Network And Switches Data Saved To merakiNetworksAndSwitches.csv")
Thanks Jimmy for the feedback, my indention has been corrected slightly (indents after the writer.writerow were one too many in) however it's still not working...
Thanks Jimmy; it was the indentation after all...