#!/usr/bin/env bash # Peripolos Collector for Linux - collects software metadata for a free Peripolos First Article Check. # # Read-only. Makes NO network connections and changes nothing on this machine. # Writes one JSON file. Review it, then upload it yourself at https://www.virtual-infra.com/peripolos.html # # Collected: distribution name and version, kernel version, installed package names and versions. # Not collected: IP or MAC addresses, user names, serial numbers, files, logs, passwords or any CUI. # The hostname is replaced with an anonymous label unless --include-hostname is given. # # Usage: ./peripolos-collector.sh [--category it|ot|edge] [--include-hostname] [--output DIR] set -eu COLLECTOR_VERSION="1.0.1" SCHEMA="peripolos-inventory/1" UPLOAD_URL="https://www.virtual-infra.com/peripolos.html" CATEGORY="it" INCLUDE_HOSTNAME=0 OUT_DIR="$(pwd)" MAX_PACKAGES=5000 while [ $# -gt 0 ]; do case "$1" in --category) CATEGORY="${2:-it}"; shift 2 ;; --include-hostname) INCLUDE_HOSTNAME=1; shift ;; --output) OUT_DIR="${2:-.}"; shift 2 ;; -h|--help) sed -n '2,12p' "$0"; exit 0 ;; *) echo "Unknown option: $1" >&2; exit 1 ;; esac done case "$CATEGORY" in it|ot|edge) ;; *) echo "--category must be it, ot or edge" >&2; exit 1 ;; esac # Escapes text for JSON escape() { value="$(printf '%s' "$1" | head -c 300)" printf '%s' "$value" | tr '\000-\037' ' ' | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' } # Removes IP addresses, MAC addresses and emails from names, then escapes for JSON (never used on versions) clean() { escape "$(printf '%s' "$1" \ | sed -E -e 's/[A-Za-z0-9._%+-]{1,64}@[A-Za-z0-9.-]{1,190}\.[A-Za-z]{2,24}/[email]/g' \ -e 's/([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}/[mac]/g' \ -e 's/(^|[^0-9A-Za-z.])([0-9]{1,3}\.){3}[0-9]{1,3}([^0-9A-Za-z.]|$)/\1[ip]\3/g')" } anon_label() { # A fresh random salt per run means the label cannot be reversed back to the hostname salt="$(cat /proc/sys/kernel/random/uuid 2>/dev/null || date +%s%N)" if command -v sha256sum >/dev/null 2>&1; then printf '%s%s' "$salt" "$(hostname)" | sha256sum | cut -c1-8 else printf '%s%s' "$salt" "$(hostname)" | cksum | cut -d' ' -f1 fi } if [ "$INCLUDE_HOSTNAME" -eq 1 ]; then LABEL="$(clean "$(hostname)")"; else LABEL="host-$(anon_label)"; fi # Read os-release as data rather than sourcing it, so it cannot change this script's variables os_field() { sed -n "s/^$1=//p" /etc/os-release 2>/dev/null | head -n1 | tr -d '"'; } OS_NAME="$(os_field NAME)"; OS_NAME="${OS_NAME:-Linux}" OS_VERSION="$(os_field VERSION_ID)" KERNEL="$(uname -r)" TMP="$(mktemp)" trap 'rm -f "$TMP"' EXIT if command -v dpkg-query >/dev/null 2>&1; then # Installed packages only ("ii"); removed packages that left config files behind are skipped dpkg-query -W -f='${db:Status-Abbrev}\t${Package}\t${Version}\n' 2>/dev/null \ | awk -F'\t' '$1 ~ /^ii/ {print $2 "\t" $3 "\t"}' > "$TMP" || true elif command -v rpm >/dev/null 2>&1; then rpm -qa --qf '%{NAME}\t%{VERSION}-%{RELEASE}\t%{VENDOR}\n' 2>/dev/null > "$TMP" || true elif command -v apk >/dev/null 2>&1; then apk info -v 2>/dev/null | sed -E 's/^(.*)-([0-9][^-]*-r[0-9]+)$/\1\t\2\t/' > "$TMP" || true fi SOFTWARE=" {\"vendor\": \"Linux\", \"product\": \"Linux kernel\", \"version\": \"$(escape "$KERNEL")\"}" COUNT=0 while IFS="$(printf '\t')" read -r pkg ver vendor; do [ -z "$pkg" ] && continue COUNT=$((COUNT + 1)) [ "$COUNT" -gt "$MAX_PACKAGES" ] && break SOFTWARE="$SOFTWARE, {\"vendor\": \"$(clean "${vendor:-}")\", \"product\": \"$(clean "$pkg")\", \"version\": \"$(escape "$ver")\"}" done < "$TMP" NOW="$(date -u +%Y-%m-%dT%H:%M:%SZ)" OUT_FILE="$OUT_DIR/peripolos-inventory-$LABEL-$(date +%Y%m%d).json" cat > "$OUT_FILE" <