snmpsim/uninstall-snmpsim-lab.sh
2026-06-05 10:58:00 -04:00

289 lines
12 KiB
Bash

#!/bin/bash
# =============================================================================
# uninstall-snmpsim-lab.sh
#
# Completely removes the SevOne SNMP Simulation Lab.
#
# Usage:
# sudo ./uninstall-snmpsim-lab.sh [OPTIONS]
#
# Options:
# --yes Skip confirmation prompt
# --keep-pip Keep the snmpsim pip package installed
# --keep-user Keep the snmpsim system user
# -h, --help Show this help
# =============================================================================
set -euo pipefail
INSTALL_DIR="/opt/snmpsim"
SNMPSIM_USER="snmpsim"
PID_DIR="/var/run/snmpsim.commands.responder"
CACHE_DIR="/tmp/snmpsim"
SERVICES=(
"snmpsim-agents.service"
"snmpsim-netflow.service"
"snmpsim-traps.timer"
"snmpsim-traps.service"
)
SERVICE_FILES=(
"/etc/systemd/system/snmpsim-agents.service"
"/etc/systemd/system/snmpsim-netflow.service"
"/etc/systemd/system/snmpsim-traps.service"
"/etc/systemd/system/snmpsim-traps.timer"
)
# ── Colours ───────────────────────────────────────────────────────────────────
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
info() { echo -e "${CYAN}[INFO]${NC} $*"; }
ok() { echo -e "${GREEN}[OK]${NC} $*"; }
skip() { echo -e "${YELLOW}[SKIP]${NC} $*"; }
section() { echo -e "\n${BLUE}══════════════════════════════════════════${NC}";
echo -e "${BLUE} $*${NC}";
echo -e "${BLUE}══════════════════════════════════════════${NC}"; }
# ── Must run as root ──────────────────────────────────────────────────────────
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}[ERROR]${NC} This script must be run as root (sudo)"
exit 1
fi
# ── Usage ─────────────────────────────────────────────────────────────────────
usage() {
cat << EOF
Usage: $(basename "$0") [OPTIONS]
OPTIONS:
--yes Skip confirmation prompt
--keep-pip Keep the snmpsim pip package installed
--keep-user Keep the snmpsim system user
-h, --help Show this help
EXAMPLES:
sudo $(basename "$0") # Full uninstall with prompt
sudo $(basename "$0") --yes # Full uninstall, no prompt
sudo $(basename "$0") --keep-pip --keep-user # Remove lab files only
EOF
exit 0
}
# ── Parse arguments ───────────────────────────────────────────────────────────
SKIP_CONFIRM=0
KEEP_PIP=0
KEEP_USER=0
for arg in "$@"; do
case "$arg" in
--yes|-y) SKIP_CONFIRM=1 ;;
--keep-pip) KEEP_PIP=1 ;;
--keep-user) KEEP_USER=1 ;;
-h|--help) usage ;;
*) echo -e "${RED}[ERROR]${NC} Unknown option: $arg"; usage ;;
esac
done
# ── Confirmation ──────────────────────────────────────────────────────────────
echo ""
echo -e "${RED}╔══════════════════════════════════════════════════╗${NC}"
echo -e "${RED}║ SevOne SNMP Lab Uninstaller ║${NC}"
echo -e "${RED}╚══════════════════════════════════════════════════╝${NC}"
echo ""
echo -e " Will remove:"
echo -e " ${CYAN}${INSTALL_DIR}/${NC} (all data, scripts, binaries)"
echo -e " ${CYAN}systemd services${NC} (snmpsim-agents, netflow, traps)"
echo -e " ${CYAN}${PID_DIR}/${NC}"
echo -e " ${CYAN}${CACHE_DIR}/${NC}"
if [[ $KEEP_PIP -eq 0 ]]; then
echo -e " ${CYAN}snmpsim pip package${NC}"
else
echo -e " ${YELLOW}snmpsim pip package${NC} (keeping -- --keep-pip)"
fi
if [[ $KEEP_USER -eq 0 ]]; then
echo -e " ${CYAN}snmpsim system user${NC}"
else
echo -e " ${YELLOW}snmpsim system user${NC} (keeping -- --keep-user)"
fi
echo ""
if [[ $SKIP_CONFIRM -eq 0 ]]; then
read -rp "Are you sure you want to uninstall? [y/N] " CONFIRM
[[ "$CONFIRM" =~ ^[Yy]$ ]] || { echo "Aborted."; exit 0; }
fi
# ─────────────────────────────────────────────────────────────────────────────
section "Step 1: Stop and disable systemd services"
# ─────────────────────────────────────────────────────────────────────────────
for svc in "${SERVICES[@]}"; do
if systemctl is-active --quiet "$svc" 2>/dev/null; then
systemctl stop "$svc"
ok "Stopped: $svc"
else
skip "Not running: $svc"
fi
if systemctl is-enabled --quiet "$svc" 2>/dev/null; then
systemctl disable "$svc"
ok "Disabled: $svc"
else
skip "Not enabled: $svc"
fi
done
# ─────────────────────────────────────────────────────────────────────────────
section "Step 2: Kill any remaining processes"
# ─────────────────────────────────────────────────────────────────────────────
if pgrep -f snmpsim-command-responder &>/dev/null; then
pkill -f snmpsim-command-responder
ok "Killed remaining snmpsim processes"
else
skip "No snmpsim processes running"
fi
if pgrep -f nflow-generator &>/dev/null; then
pkill -f nflow-generator
ok "Killed nflow-generator process"
else
skip "No nflow-generator process running"
fi
# ─────────────────────────────────────────────────────────────────────────────
section "Step 3: Remove systemd unit files"
# ─────────────────────────────────────────────────────────────────────────────
for f in "${SERVICE_FILES[@]}"; do
if [[ -f "$f" ]]; then
rm -f "$f"
ok "Removed: $f"
else
skip "Not found: $f"
fi
done
systemctl daemon-reload
ok "systemd daemon reloaded"
# ─────────────────────────────────────────────────────────────────────────────
section "Step 4: Remove install directory"
# ─────────────────────────────────────────────────────────────────────────────
if [[ -d "$INSTALL_DIR" ]]; then
rm -rf "$INSTALL_DIR"
ok "Removed: $INSTALL_DIR"
else
skip "Not found: $INSTALL_DIR"
fi
# ─────────────────────────────────────────────────────────────────────────────
section "Step 5: Remove PID directory and cache"
# ─────────────────────────────────────────────────────────────────────────────
if [[ -d "$PID_DIR" ]]; then
rm -rf "$PID_DIR"
ok "Removed: $PID_DIR"
else
skip "Not found: $PID_DIR"
fi
if [[ -d "$CACHE_DIR" ]]; then
rm -rf "$CACHE_DIR"
ok "Removed: $CACHE_DIR"
else
skip "Not found: $CACHE_DIR"
fi
# ─────────────────────────────────────────────────────────────────────────────
section "Step 6: snmpsim pip package"
# ─────────────────────────────────────────────────────────────────────────────
if [[ $KEEP_PIP -eq 1 ]]; then
skip "Keeping snmpsim pip package (--keep-pip)"
else
if pip3 show snmpsim-lextudio &>/dev/null 2>&1; then
pip3 uninstall -y snmpsim-lextudio
ok "Uninstalled: snmpsim-lextudio"
elif pip3 show snmpsim &>/dev/null 2>&1; then
pip3 uninstall -y snmpsim
ok "Uninstalled: snmpsim"
else
skip "snmpsim pip package not found"
fi
# Remove leftover snmpsim binaries
for bin in snmpsim-command-responder snmpsim-record-mibs; do
path=$(command -v "$bin" 2>/dev/null || true)
if [[ -n "$path" ]]; then
rm -f "$path"
ok "Removed binary: $path"
fi
done
fi
# ─────────────────────────────────────────────────────────────────────────────
section "Step 7: snmpsim system user"
# ─────────────────────────────────────────────────────────────────────────────
if [[ $KEEP_USER -eq 1 ]]; then
skip "Keeping snmpsim user (--keep-user)"
else
if id "$SNMPSIM_USER" &>/dev/null; then
userdel "$SNMPSIM_USER"
ok "Removed user: $SNMPSIM_USER"
else
skip "User not found: $SNMPSIM_USER"
fi
fi
# ─────────────────────────────────────────────────────────────────────────────
section "Step 8: Remove cron entries (if any)"
# ─────────────────────────────────────────────────────────────────────────────
if crontab -l 2>/dev/null | grep -q "trap-generator"; then
crontab -l 2>/dev/null | grep -v "trap-generator" | crontab -
ok "Removed trap-generator cron entry"
else
skip "No trap-generator cron entry found"
fi
# ─────────────────────────────────────────────────────────────────────────────
# Done
# ─────────────────────────────────────────────────────────────────────────────
echo ""
echo -e "${GREEN}╔══════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Uninstall complete ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════╝${NC}"
echo ""
KEPT=()
[[ $KEEP_PIP -eq 1 ]] && KEPT+=("snmpsim pip package")
[[ $KEEP_USER -eq 1 ]] && KEPT+=("snmpsim system user")
if [[ ${#KEPT[@]} -gt 0 ]]; then
echo -e " Kept (by request):"
for item in "${KEPT[@]}"; do
echo -e " ${YELLOW}${item}${NC}"
done
echo ""
fi
echo -e " Not removed (system packages):"
echo -e " Python 3, pip, Go, git, net-snmp-utils"
echo ""
echo -e " Ready for a fresh install:"
echo -e " ${CYAN}sudo ./install-snmpsim-lab.sh --vm-ip <IP> --trap-host <IP> --netflow-host <IP>${NC}"
echo ""