From 6be83bd6c137a3c4756dff3fb262fddfb8db819b Mon Sep 17 00:00:00 2001 From: Phil Date: Sun, 23 Jul 2023 11:10:39 +0100 Subject: [PATCH] Added Files --- FQDN-Updater.sh | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 FQDN-Updater.sh diff --git a/FQDN-Updater.sh b/FQDN-Updater.sh new file mode 100644 index 0000000..ea92304 --- /dev/null +++ b/FQDN-Updater.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +# Replace 'your_fqdn_here.com' with the actual FQDN you want to update +fqdn="your_fqdn_here.com" + +# Path to the log file where the IP address will be recorded +log_file="/path/to/ip_log.txt" + +# Function to resolve the IP address from FQDN +get_ip_address() { + dig +short $fqdn +} + +# Function to get the last recorded IP address from the log file +get_last_ip_address() { + if [[ -f "$log_file" ]]; then + tail -n 1 "$log_file" + else + echo "" + fi +} + +# Check if 'ufw' is installed +if ! command -v ufw &> /dev/null; then + echo "UFW is not installed. Please install it first." + exit 1 +fi + +# Check if 'dig' (from 'dnsutils') is installed +if ! command -v dig &> /dev/null; then + echo "The 'dnsutils' package is not installed. Please install it first." + exit 1 +fi + +# Enable UFW if it's not already enabled +ufw --force enable + +# Get the current and last recorded IP addresses +current_ip=$(get_ip_address) +last_ip=$(get_last_ip_address) + +if [[ -z $current_ip ]]; then + echo "Unable to resolve the IP address for '$fqdn'." + exit 1 +fi + +if [[ $current_ip != $last_ip ]]; then + # Remove the old IP address from UFW rules if it exists + if [[ -n $last_ip ]]; then + ufw delete allow from $last_ip + fi + + # Allow traffic from the obtained IP address + ufw allow from $current_ip + ufw reload + + # Log the current IP address to the file + echo "$current_ip" >> "$log_file" + + echo "UFW has been updated to allow traffic from $fqdn ($current_ip)." +else + echo "IP address for '$fqdn' has not changed. No updates needed." +fi +