#!/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"
kept=0
removed=0
no_instance=0
> "$EXISTING_FILE"
> "$REMOVED_FILE"
while IFS= read -r oid || [[ -n "$oid" ]]; do
[[ -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"
((removed++))
elif grep -qi "No Such Instance" <<< "$result"; then
echo "KEEP: $oid (object exists, instance absent)"
echo "$oid" >> "$EXISTING_FILE"
((kept++))
((no_instance++))
else
echo "KEEP: $oid"
echo "$oid" >> "$EXISTING_FILE"
((kept++))
fi
done < "$INPUT_FILE"
echo
echo "Summary:"
echo " Kept: $kept"
echo " Missing instances: $no_instance"
echo " Removed (no object): $removed"
echo " Existing OIDs file: $EXISTING_FILE"
echo " Removed OIDs file: $REMOVED_FILE"