#!/bin/bash set -euo pipefail CONF_DIR="${1:?Usage: $0 }" wgfile=${CONF_DIR}/wg0.conf # Pick a random .conf except wg0.conf random_conf=$(find "$CONF_DIR" -maxdepth 1 -type f -name "*.conf" ! -name "wg0.conf" | shuf -n1) echo "Selected: $random_conf" # Copy to wg0.conf in current directory cp "$random_conf" $wgfile # Resolve hostname in Endpoint to IP endpoint=$(grep '^Endpoint' $wgfile | awk '{print $3}') host=${endpoint%:*} port=${endpoint##*:} ip=$(getent ahostsv4 "$host" | awk '{print $1; exit}') if [[ -z "$ip" ]]; then echo "Failed to resolve $host" exit 1 fi sed -i "s|^Endpoint = .*|Endpoint = $ip:$port|" $wgfile echo "Updated Endpoint:" grep '^Endpoint' $wgfile