diff options
| author | Anton Bobov <anton@bobov.name> | 2025-02-08 00:32:20 +0500 |
|---|---|---|
| committer | Anton Bobov <anton@bobov.name> | 2025-02-08 00:32:20 +0500 |
| commit | b9bc9671d76dc99f3ba16cdf6ebfeb7d396e4cb5 (patch) | |
| tree | 5cbcc673b5b2a845705798734c5c450af5c57900 | |
| parent | f0dd30b5bdda5df01c7b341ffb4583f12ad5fc11 (diff) | |
Update RouterOS address list script
| -rwxr-xr-x | through-vpn.sh | 73 |
1 files changed, 66 insertions, 7 deletions
diff --git a/through-vpn.sh b/through-vpn.sh index 7e9e1ee..b35623b 100755 --- a/through-vpn.sh +++ b/through-vpn.sh @@ -1,11 +1,70 @@ -#!/bin/sh -# -# Add host to through-vpn address list. +#!/usr/bin/env bash +# Add host to RouterOS address list. # -if [ $# -eq 0 ] ; then - echo 'First argument must be host name.' +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 -fi + elif [ $dryrun -eq 0 ]; then + create_router_commands "$timeout" "$@" | ssh "$ROUTER_HOST" + else + create_router_commands "$timeout" "$@" + fi +} -ssh riga "/ip firewall address-list add address=$1 list=through-vpn" +main "$@" |
