camera-trng/scripts/stream-random.sh

41 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Stream random bytes from camera-qrng to the terminal.
# Start the server first: cargo run (or ./scripts/release-camera.sh if camera locked)
#
# Usage:
# ./scripts/stream-random.sh [count] # 10 lines (or 0 = infinite), no label
# ./scripts/stream-random.sh [label] [count] # label e.g. "Stream-1" so you can run in multiple terminals and see different streams
#
# Example (run in 3 different terminals to see 3 independent streams, never the same):
# Terminal 1: ./scripts/stream-random.sh "Stream-1" 0
# Terminal 2: ./scripts/stream-random.sh "Stream-2" 0
# Terminal 3: ./scripts/stream-random.sh "Stream-3" 0
PORT="${PORT:-8787}"
BYTES="${BYTES:-32}"
LABEL=""
COUNT=""
if [[ "$1" =~ ^[0-9]+$ ]]; then
COUNT="$1"
else
LABEL="$1"
COUNT="${2:-10}"
fi
COUNT="${COUNT:-10}"
if [[ -n "$LABEL" ]]; then
PREFIX="${LABEL}: "
else
PREFIX=""
fi
if [[ "$COUNT" == "0" ]]; then
echo "Streaming random (hex) to terminal${LABEL:+ as $LABEL} — Ctrl+C to stop"
while true; do echo -n "$PREFIX"; curl -s "http://127.0.0.1:${PORT}/random?bytes=${BYTES}&hex=true"; echo ""; done
else
echo "Streaming ${COUNT} x ${BYTES} bytes (hex)${LABEL:+ as $LABEL}:"
for ((i=1; i<=COUNT; i++)); do echo -n "$PREFIX"; curl -s "http://127.0.0.1:${PORT}/random?bytes=${BYTES}&hex=true"; echo ""; done
echo "Done."
fi