File size: 2,094 Bytes
aae9117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/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"