Add a Hostname & IP Address to DNSmsaq

From Sea of Fate
Jump to navigationJump to search

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"
    fi
  fi
done
  1. Print the total count

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