OPNsense API. Add adresses to Firewall

Started by _tribal_, December 28, 2024, 07:30:48 PM

Previous topic - Next topic
December 28, 2024, 07:30:48 PM Last Edit: January 04, 2025, 05:46:11 PM by _tribal_
Hi to all.
I've got this code:
#!/bin/sh

OPNSENSE_URL="https://ip:port"
API_KEY="key"
API_SECRET="secret"
ALIAS_NAME="NNN_ips"
IP_FILE="/tmp/NNN_ips.txt"

add_ip_to_alias() {
    local ip="$1"
    curl -k -X POST \
         -H "Content-Type: application/json" \
         -u "${API_KEY}:${API_SECRET}" \
         -d "{\"address\":\"${ip}\"}" \
         "${OPNSENSE_URL}/api/firewall/alias_util/add/${ALIAS_NAME}"
}

apply_changes() {
    curl -k -X POST \
        -H "Content-Type: application/json" \
        -u "${API_KEY}:${API_SECRET}" \
        -d "{}" \
        "${OPNSENSE_URL}/api/firewall/alias/reconfigure"
}

while IFS= read -r ip; do
    if [ -n "$ip" ]; then
        add_ip_to_alias "$ip"
        echo "$ip"
    fi
done < "$IP_FILE"

apply_changes

But it adds one address at a time, which is not very efficient. Is it possible to send the whole list to the API at once, so that the adding can be done in one request?