start_snmpd() {
    PID_FILE="$(mktemp)"
    CONF_FILE="$(mktemp)"

    get_random_port

    sed -e "s/@SNMP_PORT@/${SNMP_PORT}/g" debian/tests/snmpd.conf > "${CONF_FILE}"
    /usr/sbin/snmpd -C -c "${CONF_FILE}" -p "${PID_FILE}" -Ln

    trap stop_snmpd EXIT
}

get_random_port() {
    while true; do
        SNMP_PORT="$(( RANDOM + 1024 + 1 ))"
        grep -q "$(printf ":%.4X " "${SNMP_PORT}")" /proc/net/udp || break
    done
}

stop_snmpd() {
    kill -9 "$(cat "${PID_FILE}")"

    rm "${PID_FILE}"
    rm "${CONF_FILE}"
}
