#!/bin/bash

if [[ "$1" == "get" ]]; then
    # Retrieve the DNS server IP addresses from /etc/resolv.conf
    dns_servers=$(grep "nameserver" /etc/resolv.conf | awk '{print $2}')

    # Output the DNS server IP addresses
    #echo "DNS Servers:"
    echo "$dns_servers"
elif [[ "$1" == "set" ]]; then
    # Check if a list of DNS server IP addresses is provided
    if [[ -z "$2" ]]; then
        echo "Error: Please provide a space-delimited list of DNS server IP addresses."
        exit 1
    fi

    # Create a backup of the original /etc/resolv.conf file
    cp /etc/resolv.conf /etc/resolv.conf.backup

    # Replace the DNS server IP addresses in /etc/resolv.conf
    sed -i '/nameserver/d' /etc/resolv.conf
    for dns_server in $2; do
        echo "nameserver $dns_server" >> /etc/resolv.conf
    done

    #echo "DNS server IP addresses set successfully."
else
    echo "Invalid parameter. Usage: ./get_dns_servers.sh [get | set <dns_servers>]"
fi