blob: 264cd036eb420fbbcb2673896bff946644dadeb7 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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" "$@")"
|