Add a Hostname & IP Address to DNSmsaq

From Sea of Fate
Revision as of 01:20, 15 March 2025 by Sailor (talk | contribs)
Jump to navigationJump to search

Introduction

There are a few scripts that will help with the management of the DNSmasq install on NS1. We can get back to here

Details of the script

add_dns_record,sh

There is a script that can be run to add a dns record to dnsmasq

sudo ./add_dns_record.sh <hostname> <ip_address>

It will have two parameters one for hostname and the other for the IP Address of the host we would be dealing with. The code for the script

#!/bin/bash
#
# Script to add or update a DNS record in dnsmasq
#
# Get hostname and IP address from command line
HOSTNAME="$1"
IP_ADDRESS="$2"
#
# Check if parameters are provided
if [ -z "$HOSTNAME" ] || [ -z "$IP_ADDRESS" ]; then
  echo "Usage: sudo $0 <hostname> <ip_address>"
  exit 1
fi
#
# Define filename
FILENAME="/etc/dnsmasq.d/$HOSTNAME.seaoffate.local"
#
# Create DNS and PTR records
DNS_RECORD="address=/$HOSTNAME.seaoffate.local/$IP_ADDRESS"
#
# Trim leading/trailing spaces from IP_ADDRESS
IP_ADDRESS="${IP_ADDRESS#"${IP_ADDRESS%%[![:space:]]*}"}" # Remove leading spaces
IP_ADDRESS="${IP_ADDRESS%"${IP_ADDRESS##*[![:space:]]}"}" # Remove trailing spaces
#
# Reverse IP for PTR Record.
REVERSE_IP=$(echo "$IP_ADDRESS" | awk -F. '{print $4"."$3"."$2"."$1}')
#
# Trim leading/trailing spaces from REVERSE_IP
REVERSE_IP="${REVERSE_IP#"${REVERSE_IP%%[![:space:]]*}"}" # Remove leading spaces
REVERSE_IP="${REVERSE_IP%"${REVERSE_IP##*[![:space:]]}"}" # Remove trailing spaces
#
PTR_RECORD="ptr-record=$REVERSE_IP.in-addr.arpa,$HOSTNAME.seaoffate.local"
#
# Write records to file, overwriting any existing content
echo "$DNS_RECORD" | sudo tee "$FILENAME"
echo "$PTR_RECORD" | sudo tee -a "$FILENAME"
#
# Restart dnsmasq
sudo systemctl restart dnsmasq
#
echo "DNS record added/updated for $HOSTNAME.seaoffate.local."
echo "IP address: $IP_ADDRESS"
#
# Set permissions on the file.
sudo chmod 644 "$FILENAME"
#
echo "Permissions set to 644 on $FILENAME"
#
# List the file with its permissions
echo "\nFile details:"
ls -l "$FILENAME"
#
# Restart dnsmasq again
sudo systemctl restart dnsmasq
#
echo "dnsmasq restarted again to ensure changes are applied."
#

list_dns_entries.sh

as a quick check to make sure dns looks right there is a quick list of all dns entries called with

sudo ./list_dns_entries.sh
#!/bin/bash
#
# Script to list all DNS entries from dnsmasq configuration files with aligned IP addresses.
#
# Directory containing dnsmasq configuration files
DNSMASQ_DIR="/etc/dnsmasq.d/"
#
# Check if the directory exists
if [ ! -d "$DNSMASQ_DIR" ]; then
  echo "Error: Directory '$DNSMASQ_DIR' not found."
  exit 1
fi
#
# Function to calculate padding
calculate_padding() {
  local fqdn="$1"
  local max_fqdn_length="$2"
  local padding_length=$((max_fqdn_length - ${#fqdn}))
  local padding="" 
#
  for ((i=0; i<padding_length; i++)); do
    padding+="-"
  done
  echo "$padding"
}
#
# Find the maximum FQDN length
max_fqdn_length=0
for file in "$DNSMASQ_DIR"*; do
  if [ -f "$file" ]; then
    if grep -q "address=" "$file"; then
      fqdn=$(grep "address=" "$file" | awk -F"/" '{print $2}')
      if [ ${#fqdn} -gt $max_fqdn_length ]; then
        max_fqdn_length=${#fqdn}
      entry_count=$((entry_count + 1)) # Increment counter
      fi
    fi
  fi
done
#
# Loop through and print the entries
for file in "$DNSMASQ_DIR"*; do
  if [ -f "$file" ]; then
    if grep -q "address=" "$file"; then
      fqdn=$(grep "address=" "$file" | awk -F"/" '{print $2}')
      ip=$(grep "address=" "$file" | awk -F"/" '{print $3}')
      padding=$(calculate_padding "$fqdn" "$max_fqdn_length")
      echo "-- $fqdn$padding ------------ $ip"
      echo " "
    fi
  fi
done
  1. Print the total count

echo " " echo "Total DNS entries: $entry_count"

Delete a Record

The third thing we will want to do from time to time is to delete a record. Call this with the hostname of the dns entry that is to be removed

sudo ./delete_dns_record.sh hostname

hostname can be either FQDN or just the hostname.

!/bin/bash
#
# Script to delete a DNS record from dnsmasq configuration files.
#
# Function to prompt for confirmation
confirm_delete() {
  read -p "Are you sure you want to delete $1? (y/n): " choice
  if [[ "$choice" =~ ^[yY]$ ]]; then
    return 0
  elif [[ "$choice" =~ ^[nN]$ ]]; then
    return 1
  else
    echo "Invalid input. Deletion cancelled."
    return 1
  fi
}
#
# Check if a hostname is provided
if [ -z "$1" ]; then
  echo "Usage: $0 <hostname>"
  exit 1
fi
#
HOSTNAME="$1"
FILENAME="/etc/dnsmasq.d/$HOSTNAME"
#
# Check if the file exists with the exact hostname
if [ ! -f "$FILENAME" ]; then
  # If not, try appending .seaoffate.local
  FILENAME="/etc/dnsmasq.d/${HOSTNAME}.seaoffate.local"
  if [ ! -f "$FILENAME" ]; then
    echo "Error: DNS record for $HOSTNAME or ${HOSTNAME}.seaoffate.local not fo>
    exit 1
  fi
fi
# Display the record
#echo "Record to delete:"
cat "$FILENAME"
#
# Prompt for confirmation
#echo "DEBUG: Calling confirm_delete with: $HOSTNAME"
confirm_delete "$HOSTNAME"
CONFIRM_RESULT=$?
#echo "DEBUG: confirm_delete returned: $CONFIRM_RESULT"
#
if [ "$CONFIRM_RESULT" -ne 0 ]; then
#    echo "DEBUG: confirm_result was not zero"
    echo "Deletion cancelled."
    exit 0
fi
#
# Prompt for second confirmation only if the first was yes, and the filename en>
if [ "${FILENAME}" == "/etc/dnsmasq.d/${HOSTNAME}.seaoffate.local" ]; then
#    echo "DEBUG: Filename ends with .seaoffate.local, calling confirm_delete w>
    confirm_delete "${HOSTNAME}.seaoffate.local"
    CONFIRM_RESULT=$?
#    echo "DEBUG: confirm_delete returned: $CONFIRM_RESULT"
    if [ "$CONFIRM_RESULT" -ne 0 ]; then
        echo "DEBUG: confirm_result was not zero"
        echo "Deletion cancelled."
        exit 0
    fi
#    echo "DEBUG: confirm_result was zero"
fi
#
# Delete the file
rm "$FILENAME"
if [ $? -eq 0 ]; then
  echo "DNS record for $HOSTNAME deleted successfully."
  # Restart dnsmasq
  systemctl restart dnsmasq
  if [ $? -eq 0 ]; then
    echo "dnsmasq restarted."
  else
    echo "Error: Failed to restart dnsmasq."
  fi
else
  echo "Error: Failed to delete DNS record for $HOSTNAME."
fi