The first line of the comment had a typo in it.
Darryl L. Pierce
2010-Jan-14 20:17 UTC
[Ovirt-devel] [PATCH] Prevents duplicate DNS and NTP server entries.
Checks any entered address for DNS and NTP servers against previously entered ones in order to prevent duplicates. Resolves: rhbz#555373 Signed-off-by: Darryl L. Pierce <dpierce at redhat.com> --- scripts/ovirt-config-networking | 41 ++++++++++++++++++++++++-------------- scripts/ovirt-functions | 7 ++++++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/scripts/ovirt-config-networking b/scripts/ovirt-config-networking index ec154c2..1342a0a 100755 --- a/scripts/ovirt-config-networking +++ b/scripts/ovirt-config-networking @@ -358,7 +358,8 @@ function configure_dns while true; do printf "\n" read -ep "Please enter the ${dns} DNS server (or ENTER to exit): " - if [[ -z "${REPLY}" ]]; then + local ADDRESS=$(trim_whitespace "$REPLY") + if [[ -z "${ADDRESS}" ]]; then if [[ -z "${DNS}" ]]; then printf "\nAborted...\n" return @@ -366,15 +367,20 @@ function configure_dns break fi fi - if is_valid_ipv4 $REPLY; then + if is_valid_ipv4 $ADDRESS; then if [[ -z "${DNS}" ]]; then - DNS="${REPLY}" - elif [[ -n "${REPLY}" ]]; then - DNS="${DNS}:${REPLY}" + DNS="${ADDRESS}" + break + elif [[ -n "${ADDRESS}" ]]; then + if [[ ! $DNS =~ $ADDRESS ]]; then + DNS="${DNS}:${ADDRESS}" + break + else + printf "${ADDRESS} is already defined as a DNS server.\n" + fi fi - break else - printf "${REPLY} is an invalid address.\n" + printf "${ADDRESS} is an invalid address.\n" fi done done @@ -403,24 +409,29 @@ function configure_ntp { local AUTO=$2 if [[ "$AUTO" == "AUTO" && -n "$OVIRT_NTP" ]]; then - NTPSERVERS=$OVIRT_NTP + NTPSERVERS=$OVIRT_NTP else - NTPSERVERS="" + NTPSERVERS="" fi if [ -z "$AUTO" ]; then if has_configured_interface true; then - while true; do - read -ep "Enter an NTP server (hit return when finished): " + while true; do + read -ep "Enter an NTP server (hit return when finished): " + local address=$(trim_whitespace "$REPLY") - if [ -z "$REPLY" ]; then break; fi + if [ -z "$address" ]; then break; fi - if is_valid_ipv4 $REPLY; then - NTPSERVERS="${NTPSERVERS}:${REPLY}" + if is_valid_ipv4 $address; then + if [[ $NTPSERVERS =~ $address ]]; then + printf "${address} is already defined as an NTP server.\n" + else + NTPSERVERS="${NTPSERVERS}:${REPLY}" + fi else printf "${REPLY} is an invalid address.\n" fi - done + done fi fi } diff --git a/scripts/ovirt-functions b/scripts/ovirt-functions index 6435387..0d47dc8 100644 --- a/scripts/ovirt-functions +++ b/scripts/ovirt-functions @@ -598,6 +598,13 @@ add_if_not_exist() { || echo "$string" >> "$file" } +# $1 - the string to be trimmed +trim_whitespace () { + local text=${1} + + printf "$text" | awk '{gsub(/^[ ]*/,"",$0); gsub(/[ ]*$/,"",$0) ; print }' +} + is_numeric() { printf "$1" | grep -q -E '^[0-9]+$' } -- 1.6.5.2