Created
April 18, 2024 12:05
-
-
Save yujiterada/bc3a585312b8c96aca12aa82a6c381d4 to your computer and use it in GitHub Desktop.
Reboot Meraki devices based on CSV file
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
import csv | |
import meraki | |
import os | |
from meraki.exceptions import APIError | |
# Constants | |
API_KEY = os.environ.get('MERAKI_API_KEY') # Replace with your Meraki Dashboard API key | |
GROUP_NUMBER = 1 # The target group number to check for | |
INPUT_CSV_FILE = 'input.csv' # Path to the input CSV file | |
OUTPUT_CSV_FILE = 'output.csv' # Path to the output CSV file | |
# Initialize a Meraki dashboard API session | |
dashboard = meraki.DashboardAPI(API_KEY, output_log=False, suppress_logging=True) | |
def reboot_device(serial): | |
try: | |
response = dashboard.devices.rebootDevice(serial=serial) | |
return 'Success', response | |
except APIError as e: | |
return 'Fail', str(e) | |
def process_devices(): | |
results = [] | |
with open(INPUT_CSV_FILE, mode='r', newline='') as file: | |
reader = csv.DictReader(file) | |
for row in reader: | |
if int(row['group_number']) == GROUP_NUMBER: | |
status, response = reboot_device(row['serial_number']) | |
results.append({ | |
'serial_number': row['serial_number'], | |
'status': status, | |
'response': response | |
}) | |
with open(OUTPUT_CSV_FILE, mode='w', newline='') as file: | |
fieldnames = ['serial_number', 'status', 'response'] | |
writer = csv.DictWriter(file, fieldnames=fieldnames) | |
writer.writeheader() | |
writer.writerows(results) | |
for result in results: | |
print(result) | |
if __name__ == '__main__': | |
process_devices() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment