#!/usr/bin/env bash
set -u
if [[ $# -ne 4 ]]; then
echo "Usage: $0 <input_file> <agent_address> <existing_oids_file> <removed_oids_file>"
exit 1
fi
INPUT_FILE="$1"
AGENT="$2"
EXISTING_FILE="$3"
REMOVED_FILE="$4"
COMMUNITY="public"
> "$EXISTING_FILE"
> "$REMOVED_FILE"
while IFS= read -r oid || [[ -n "$oid" ]]; do
# Skip empty lines
[[ -z "$oid" ]] && continue
result=$(snmpget -v2c -c "$COMMUNITY" "$AGENT" "$oid" 2>&1)
if grep -qi "No Such Object" <<< "$result"; then
echo "REMOVE: $oid"
echo "$oid | $result" >> "$REMOVED_FILE"
else
# Keep normal results AND "No Such Instance"
echo "$oid" >> "$EXISTING_FILE"
if grep -qi "No Such Instance" <<< "$result"; then
echo "KEEP: $oid (object exists, instance absent)"
else
echo "KEEP: $oid"
fi
fi
done < "$INPUT_FILE"