summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xros-al-add56
-rwxr-xr-xthrough-vpn.sh70
2 files changed, 56 insertions, 70 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" "$@")"
diff --git a/through-vpn.sh b/through-vpn.sh
deleted file mode 100755
index b35623b..0000000
--- a/through-vpn.sh
+++ /dev/null
@@ -1,70 +0,0 @@
-#!/usr/bin/env bash
-# Add host to RouterOS address list.
-#
-
-set -euo pipefail
-
-LIST_NAME="through-vpn"
-ROUTER_HOST="riga"
-
-usage() {
- cat <<EOF
-Usage: ${0##*/} [-t|-n|-h] name...
-
-Name could be a domain name or URL.
-
-Options:
- -h print this help
- -t timeout value (ex: 1m, 8h)
- -n dry run, just print commands
-EOF
-}
-
-extract_domain() {
- cat <<EOF | python3
-from urllib.parse import urlparse
-
-url = urlparse(r'$1')
-print(url.hostname or '$1')
-EOF
-}
-
-create_router_commands() {
- local timeout="$1"
- shift
- for name in "$@"; do
- domain=$(extract_domain "$name")
- echo "/ip firewall address-list add address=$domain list=$LIST_NAME timeout=$timeout"
- done
-}
-
-main() {
- local timeout=
- local dryrun=0
- while getopts ":nt:" opt; do
- case "${opt}" in
- t)
- timeout="${OPTARG}"
- ;;
- n)
- dryrun=1
- ;;
- *)
- usage
- exit
- ;;
- esac
- done
- shift $((OPTIND - 1))
-
- if [ $# -eq 0 ]; then
- echo 'First argument must be domain or URL'
- exit 1
- elif [ $dryrun -eq 0 ]; then
- create_router_commands "$timeout" "$@" | ssh "$ROUTER_HOST"
- else
- create_router_commands "$timeout" "$@"
- fi
-}
-
-main "$@"