#!/usr/bin/env bash set -euo pipefail usage() { cat <<'EOF' Batch-run cicflowmeter over each immediate subfolder in an input directory. Default: ./cicflowmeter_batch.sh Options: -i, --input Input root directory containing subfolders of pcaps (default: ./PCAP) -o, --output Output root directory for CSVs (default: ./csv_output) -f, --flat Write all CSV outputs directly into --output (no per-subfolder directory) -h, --help Show help Examples: ./cicflowmeter_batch.sh -i ./PCAP -o ./csv_output ./cicflowmeter_batch.sh -i ./PCAP -o ./csv --flat Notes: - Requires 'cicflowmeter' to be on PATH (or set CICFLOWMETER_CMD to override). - The script only processes immediate subfolders of the input root. EOF } INPUT_DIR="./PCAP" OUTPUT_DIR="./csv_output" FLAT=0 while [[ $# -gt 0 ]]; do case "$1" in -i|--input) INPUT_DIR="${2:-}"; shift 2 ;; -o|--output) OUTPUT_DIR="${2:-}"; shift 2 ;; -f|--flat) FLAT=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Unknown argument: $1" >&2 usage >&2 exit 2 ;; esac done CMD="${CICFLOWMETER_CMD:-cicflowmeter}" if ! command -v "$CMD" >/dev/null 2>&1; then echo "Error: '$CMD' not found on PATH. Install cicflowmeter or set CICFLOWMETER_CMD." >&2 exit 127 fi if [[ ! -d "$INPUT_DIR" ]]; then echo "Error: input directory not found: $INPUT_DIR" >&2 exit 1 fi mkdir -p "$OUTPUT_DIR" shopt -s nullglob processed=0 skipped=0 for dir in "$INPUT_DIR"/*/; do [[ -d "$dir" ]] || continue # Only process folders that contain at least one pcap/pcapng. pcaps=( "$dir"/*.pcap "$dir"/*.pcapng ) if [[ ${#pcaps[@]} -eq 0 ]]; then ((skipped+=1)) continue fi name="$(basename "${dir%/}")" if [[ "$FLAT" -eq 1 ]]; then out="$OUTPUT_DIR" else out="$OUTPUT_DIR/$name" mkdir -p "$out" fi echo "==> Processing: $dir" echo " Output to: $out" "$CMD" -d "$dir" -c "$out" ((processed+=1)) done echo "" echo "Done." echo "Processed folders: $processed" echo "Skipped folders: $skipped"