wireguard cron
定时修改 wireguard 端口
details
# echo "Enter your new listenPort:"
# read ListenPort
# # Check if the input is a valid number
# if ! [[ $ListenPort =~ ^[0-9]+$ ]]; then
# echo "Invalid listenPort Please enter a valid number."
# else
# # Print the number
# echo "The new listenPort you entered is: $ListenPort"
# fi
# modify /etc/wireguard/wg0.conf ListenPort field
# ==================================
# ListenPort=6392
# ==================================
# Replace the ListenPort value in /etc/wireguard/wg0.conf
PreviousListenPort=$(grep '^ListenPort' /etc/wireguard/wg0.conf | cut -d " " -f 3)
defaultNextPort=$((PreviousListenPort + 1))
sed -i "s/ListenPort.*/ListenPort = $defaultNextPort/g" /etc/wireguard/wg0.conf
# Verify the change
grep "ListenPort" /etc/wireguard/wg0.conf
# ufw status
# ufw delete allow $PreviousListenPort/udp
# ufw allow $defaultNextPort/udp
# ufw status
wg-quick down /etc/wireguard/wg0.conf
wg-quick up /etc/wireguard/wg0.conf