Commit 0b20bdb1 authored by Vitaly Lipatov's avatar Vitaly Lipatov

functions: add shared helpers (log, read_value, ipcmd_for, resolve_gw, etc.)

Extract common functions used by both route-update.sh and route-health.sh: - log, read_value, read_values, ipcmd_for, has_option, rule_pref - table_by_name, resolve_default_gw, resolve_gw Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 99323908
# --- Shared helpers (used by route-update.sh, route-health.sh) ---
log() { echo "$(date '+%H:%M:%S') $*" ; }
# Read first non-comment non-empty line from a config file
read_value() { grep -v '^#' "$1" 2>/dev/null | grep -m1 . ; }
# Read all non-comment non-empty lines from a config file
read_values() { grep -v '^#' "$1" 2>/dev/null | grep . ; }
# Determine ip command for routes directory ("ip" or "ip -6")
ipcmd_for() { case "$1" in *routes6*) echo "ip -6" ;; *) echo "ip" ;; esac ; }
# Check if option is set in group's options file
has_option() { [ -f "$1/options" ] && grep -q "^$2$" "$1/options" 2>/dev/null ; }
# Get rule priority from table number (table × 10)
rule_pref() { echo $(( $1 * 10 )) ; }
# Look up routing table number by name in /etc/iproute2/rt_tables
table_by_name() { awk -v n="$1" '$2 == n { print $1; exit }' /etc/iproute2/rt_tables 2>/dev/null ; }
# Resolve "default" keyword to actual default gateway
resolve_default_gw() { $1 route show default | awk '/default/ {print $3; exit}' ; }
# Resolve gateway value: "default" → system default, hostname → IP, IP → as-is
resolve_gw()
{
local val="$1"
local ipcmd="${2:-ip}"
case "$val" in
default) resolve_default_gw "$ipcmd" ;;
*[a-zA-Z]*)
if [ "$ipcmd" = "ip -6" ] ; then
dig +short AAAA "$val" | grep -m1 ':'
else
dig +short A "$val" | grep -m1 '[0-9]'
fi ;;
*) echo "$val" ;;
esac
}
# --- DNS resolution ---
get_ipv4_list() get_ipv4_list()
{ {
echo "$1" | grep -q "[a-z]" || return 0 echo "$1" | grep -q "[a-z]" || return 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment