A collection of utility scripts for Cloudflare API operations
This repository contains a curated collection of shell scripts designed to simplify and automate common Cloudflare API operations. Whether you're managing Logpush jobs, monitoring email security, or testing image upload performance, these utilities provide ready-to-use solutions for your Cloudflare infrastructure needs.
- Mac, Linux (for shell scripts)
- Windows (for PowerShell scripts)
curl- for API callsjq- for JSON parsingperl- for time measurements (some scripts)
Set the following environment variables as needed for each script:
# Cloudflare basic credentials
export CLOUDFLARE_EMAIL="your-email@example.com"
export CLOUDFLARE_APIKEY="your-api-key"
export CLOUDFLARE_ACCOUNT_ID="your-account-id"
export CLOUDFLARE_AUTH_TOKEN="your-auth-token"
# R2 access credentials (for Logpush/log query)
export R2_ACCESS_KEY_ID="your-r2-access-key-id"
export R2_SECRET_ACCESS_KEY="your-r2-secret-access-key"
# Email Security credentials
export CLOUDFLARE_AREA1_PUBKEY="your-area1-public-key"
export CLOUDFLARE_AREA1_PRIKEY="your-area1-private-key"-
Clone the repository
git clone https://github.com/yourusername/nb-util4cf.git cd nb-util4cf -
Set up environment variables
# Export required credentials based on which scripts you'll use export CLOUDFLARE_EMAIL="your-email@example.com" export CLOUDFLARE_APIKEY="your-api-key" export CLOUDFLARE_ACCOUNT_ID="your-account-id"
-
Make scripts executable
chmod +x *.sh -
Run a script
# Example: List Logpush jobs ./list_logpush_jobs.sh
Lists all registered Logpush jobs.
./list_logpush_jobs.shDisplays the list of fields for a specified dataset.
./list_logpush_dataset_fields.sh <Dataset Name>Arguments:
Dataset Name: Name of the dataset (e.g.,zero_trust_network_sessions)
Adds an account-level Logpush job to R2.
./add_logpush_job.sh <Dataset Name> <Bucket Name> <Job Name>Arguments:
Dataset Name: Name of the dataset (e.g.,zero_trust_network_sessions)Bucket Name: R2 bucket nameJob Name: Logpush job name
Queries logs from R2.
./log_query.sh <Bucket Name> <Prefix> <Start Time> [End Time]Arguments:
Bucket Name: R2 bucket namePrefix: Prefix in R2 bucket (e.g.,{DATE})Start Time: Query start time (e.g.,2023-05-04T16:00:00Z)End Time: Query end time (defaults to current time if omitted, e.g.,2023-05-06T16:00:00Z)
Retrieves a list of quarantined emails from Cloudflare Email Security.
./get_quarantined_mails.sh <Since> <End> [Limit]Arguments:
Since: Start date (e.g.,20230901)End: End date (e.g.,20231231)Limit: Fetch limit (default:0- no limit)
JSON to CSV conversion example:
./get_quarantined_mails.sh 20230901 20231231 | jq -r '.data[]|[.subject, .from, .to[], .detection_reasons[],.is_quarantined]|@csv'Uploads images using the Direct Upload API and performs performance testing.
./upload_image_direct.sh <Image Path> <Repeat Number> <Interval> <Mode> [Delete Option]Arguments:
Image Path: Local file path for the input imageRepeat Number: Number of times to repeat upload (0: infinite loop)Interval: Interval between loops in secondsMode: API call mode (generalorbatch)Delete Option: Whether to delete uploaded files (Y: delete,N: keep, default:Y)
Note: For infinite loop (NUM = 0), interval must be 60 seconds or more.
Uploads images via URL and performs performance testing.
./upload_image_via_url.sh <Image URL> <Repeat Number> <Interval> <Mode> [Delete Option]Arguments:
Image URL: URL of the input imageRepeat Number: Number of times to repeat upload (0: infinite loop)Interval: Interval between loops in secondsMode: API call mode (generalorbatch)Delete Option: Whether to delete uploaded files (Y: delete,N: keep, default:Y)
Note: For infinite loop (NUM = 0), interval must be 60 seconds or more.
Periodically checks the response time of a specified URL (PowerShell).
.\get_response_time.ps1 <URL> <Interval>Arguments:
URL: Target URL to checkInterval: Check interval in seconds
Output files:
response_time.txt: Log of successful responsesresponse_error.txt: Error log
This project is licensed under the MIT License - see the LICENSE file for details.
Copyright (c) 2023 Shunjiro Yatsuzuka