#!/usr/bin/env bash # Usage: # configLine [searchPattern] [replaceLine] [filePath] # Ref https://stackoverflow.com/a/54909102/1460422 config_line() { local OLD_LINE_PATTERN NEW_LINE NEW FILE OLD_LINE_PATTERN="$1"; shift NEW_LINE="$1"; shift FILE="$1" NEW="$(echo "${NEW_LINE}" | sed 's/\//\\\//g')"; touch "${FILE}" sed -i '/'"${OLD_LINE_PATTERN}"'/{s/.*/'"${NEW}"'/;h};${x;/./{x;q100};x}' "${FILE}" if [[ $? -ne 100 ]] && [[ ${NEW_LINE} != '' ]] then echo "${NEW_LINE}" >> "${FILE}" fi } log_msg() { echo -e "[ ${HOSTNAME} ] [ $(date -u +"%Y-%m-%dT%H:%M:%SZ") ] $*" >&2; } # Old approach - we've learned we can override /etc/network/interfaces via /etc/dhcp/dhclient.conf # config_line "dns-nameservers" "dns-nameservers 172.16.230.100" "/etc/network/interfaces"; completed_file="/etc/dhcp/sbrl-dns-configured"; log_msg "Configuring DNS nameserver"; if [[ ! -f "${completed_file}" ]]; then log_msg "DNS nameserver not yet configured, appending to /etc/dhcp/dhclient.conf"; echo "interface \"eth0\" { supersede domain-name-servers 172.16.230.100; }" | sudo tee -a /etc/dhcp/dhclient.conf; sudo touch "${completed_file}"; log_msg "Complete, this machine needs a reboot to activate the new config directives"; else log_msg "DNS nameserver config written already, no changes made"; fi