Skip to content

nasebanal/nb-util4cf

Β 
Β 

Repository files navigation

NASEBANAL Utility for Cloudflare

A collection of utility scripts for Cloudflare API operations

πŸ“‹ Table of Contents

πŸš€ Overview

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.

πŸ“¦ Requirements

Platform

  • Mac, Linux (for shell scripts)
  • Windows (for PowerShell scripts)

Commands

  • curl - for API calls
  • jq - for JSON parsing
  • perl - for time measurements (some scripts)

Environment Variables

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"

🏁 Getting Started

  1. Clone the repository

    git clone https://github.com/yourusername/nb-util4cf.git
    cd nb-util4cf
  2. 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"
  3. Make scripts executable

    chmod +x *.sh
  4. Run a script

    # Example: List Logpush jobs
    ./list_logpush_jobs.sh

πŸ“š Scripts

πŸ“Š Logpush

list_logpush_jobs.sh

Lists all registered Logpush jobs.

./list_logpush_jobs.sh

list_logpush_dataset_fields.sh

Displays 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)

add_logpush_job.sh

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 name
  • Job Name: Logpush job name

log_query.sh

Queries logs from R2.

./log_query.sh <Bucket Name> <Prefix> <Start Time> [End Time]

Arguments:

  • Bucket Name: R2 bucket name
  • Prefix: 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)

πŸ“§ Email Security

get_quarantined_mails.sh

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'

πŸ–ΌοΈ Images

upload_image_direct.sh

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 image
  • Repeat Number: Number of times to repeat upload (0: infinite loop)
  • Interval: Interval between loops in seconds
  • Mode: API call mode (general or batch)
  • 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.

upload_image_via_url.sh

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 image
  • Repeat Number: Number of times to repeat upload (0: infinite loop)
  • Interval: Interval between loops in seconds
  • Mode: API call mode (general or batch)
  • 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.

πŸ› οΈ Utilities

get_response_time.ps1

Periodically checks the response time of a specified URL (PowerShell).

.\get_response_time.ps1 <URL> <Interval>

Arguments:

  • URL: Target URL to check
  • Interval: Check interval in seconds

Output files:

  • response_time.txt: Log of successful responses
  • response_error.txt: Error log

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘€ Author

Copyright (c) 2023 Shunjiro Yatsuzuka

About

A curated collection of shell scripts for automating Cloudflare API operations. Manage Logpush jobs, monitor email security, test image upload performance, and query logs from R2 with ease.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Shell 94.8%
  • PowerShell 5.2%