diff options
Diffstat (limited to 'ros-al-add')
| -rwxr-xr-x | ros-al-add | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/ros-al-add b/ros-al-add new file mode 100755 index 0000000..264cd03 --- /dev/null +++ b/ros-al-add @@ -0,0 +1,56 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# @describe Adds domains or URLs to a RouterOS address list +# @meta require-tools python3,host,ssh +# @meta combine-shorts +# +# @arg hosts+ Domain names or URLs to add to the address list +# @option -t --timeout Timeout for address list entries (e.g. '1d', '2h'). Permanent if omitted +# @flag -a --all-ips Resolve domain to all A records and add individual IPs +# @flag -n --dry-run Show generated commands without executing them +# +# @env ROS_AL_ADD_HOST! RouterOS device host +# @env ROS_AL_ADD_LIST_NAME! Name of address list in RouterOS +# @env ROS_AL_ADD_COMMENT_PREFIX Optional prefix for address list comments (e.g., 'vpn: ') + +extract_domain() { + cat <<EOF | python3 +from urllib.parse import urlparse + +url = urlparse(r'$1') +print(url.hostname or '$1') +EOF +} + +extract_all_ips() { + host -t A "$1" | awk '/has address/ {print $4}' +} + +create_router_commands() { + local timeout="${argc_timeout:-}" + local domain comment + for name in "$@"; do + domain=$(extract_domain "$name") + comment="${ROS_AL_ADD_COMMENT_PREFIX:-}domain=$domain" + if [ -n "${argc_all_ips:-}" ]; then + for address in $(extract_all_ips "$domain"); do + echo "/ip firewall address-list add address=$address list=$ROS_AL_ADD_LIST_NAME comment=\"$comment\" timeout=$timeout" + done + fi + echo "/ip firewall address-list add address=$domain list=$ROS_AL_ADD_LIST_NAME comment=\"$comment\" timeout=$timeout" + done +} + +main() { + local commands + commands=$(create_router_commands "$@") + if [ -n "${argc_dry_run:-}" ]; then + echo "$commands" + else + echo "$commands" | ssh "$ROS_AL_ADD_HOST" + fi +} + +eval "$(argc --argc-eval "$0" "$@")" |
