From 77d8e3dd7ce30aa72f1fac9629bc52e31459c7a1 Mon Sep 17 00:00:00 2001 From: Tristan Ancelet Date: Sun, 12 Nov 2023 17:08:54 -0600 Subject: [PATCH] Reworked it --- utils/hosts.sh | 16 ++++++++++++ utils/shared-networks.sh | 55 ++++++++-------------------------------- utils/subnets.sh | 25 ++++++++---------- 3 files changed, 37 insertions(+), 59 deletions(-) create mode 100755 utils/hosts.sh mode change 100644 => 100755 utils/subnets.sh diff --git a/utils/hosts.sh b/utils/hosts.sh new file mode 100755 index 0000000..614cab6 --- /dev/null +++ b/utils/hosts.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +get_host_declerations () { + local SECTION_CONTENT SECTION_NAME + local -n HASH_TABLE="${1:?"get_subnet_declerations: Hash table was not provided"}" + local CONTENT="${2:?"get_subnet_declerations: Content to be parsed was not provided"}" + local -a SECTIONS + + get_sections $HOST_BEGIN_REGEX SECTIONS "$CONTENT" + + for i in ${!SECTIONS[@]}; do + SECTION_CONTENT="${SECTIONS[$i]}" + SECTION_NAME=`grep "host" <<< "$SECTION_CONTENT" | awk '{ print $2 }'` + HASH_TABLE["$SECTION_NAME"]="${SECTION_CONTENT}" + done +} diff --git a/utils/shared-networks.sh b/utils/shared-networks.sh index 3467458..2181360 100755 --- a/utils/shared-networks.sh +++ b/utils/shared-networks.sh @@ -1,53 +1,18 @@ #!/bin/bash -# BEGIN: Variables - -DHCPD_CONF_CONTENTS="$(<${CONFIG_FILE:?"DHCPD_CONFIG was not set"})" - -# END: Variables - - -# BEGIN: Functions - get_shared_network_declarations () { - local CURRENT_BUFFER="" - local CURRENT_SHARED_NETWORK_NAME="" - local -i OPEN_BRACKETS=0 - local -i CLOSE_BRACKETS=0 - local CAN_READ=0 - local -n HASH_TABLE="${1:?"get_declarations: Has table variable not passed into function"}" + local -n HASH_TABLE="${1:?"get_shared_network_declarations: Hash table variable not passed into function"}" + local CONTENT="${2:?"get_shared_network_declarations: Content was not provided"}" + local -a SECTIONS - while read line; do - if [[ "$line" =~ ^$SHARED_NETWORK_BEGIN_REGEX.*$ ]]; then - CAN_READ=1 - CURRENT_SHARED_NETWORK_NAME=`echo $line | awk '{ print $2 }' | tr -d \"` - fi + local SECTION_CONTENT SECTION_NAME - if [[ $CAN_READ -eq 1 ]]; then - CURRENT_BUFFER+="$line\n" - [[ "$line" == *'{'* ]] && ((OPEN_BRACKETS++)) - [[ "$line" == *'}'* ]] && ((CLOSE_BRACKETS++)) - if [[ $OPEN_BRACKETS -eq $CLOSE_BRACKETS ]]; then - HASH_TABLE["$CURRENT_SHARED_NETWORK_NAME"]="$( echo -e "$CURRENT_BUFFER" )" - CURRENT_BUFFER="" - CAN_READ=0 - OPEN_BRACKETS=0 - CLOSE_BRACKETS=0 - CURRENT_SHARED_NETWORK_NAME="" - fi - fi + get_sections "$SHARED_NETWORK_BEGIN_REGEX" SECTIONS "$CONTENT" - done <<< "$DHCPD_CONF_CONTENTS" + for i in ${!SECTIONS[@]}; do + SECTION_CONTENT="${SECTIONS[$i]}" + SECTION_NAME=`grep -Eo $SHARED_NETWORK_BEGIN_REGEX <<< "$SECTION_CONTENT" | awk '{ print $2 }' | tr -d \" ` + HASH_TABLE[$SECTION_NAME]="$SECTION_CONTENT" + done } -# END: Functions - -declare -A testHashTable - -get_shared_network_declarations testHashTable - -for name in "${!testHashTable[@]}"; do - echo "$name" - echo "${testHashTable[$name]}" -done - diff --git a/utils/subnets.sh b/utils/subnets.sh old mode 100644 new mode 100755 index 7ac9c1e..05d9d0c --- a/utils/subnets.sh +++ b/utils/subnets.sh @@ -1,20 +1,17 @@ #!/bin/bash - -# BEGIN: Variables - -SUBNET_BEGIN_REGEX='^[[:space:]]*subnet[[:space:]]+[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[[:space:]]+netmask[[:space:]]+[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[[:space:]]+\{' -# END: Variables - get_subnet_declerations () { + local SECTION_CONTENT SECTION_NAME local -n HASH_TABLE="${1:?"get_subnet_declerations: Hash table was not provided"}" local CONTENT="${2:?"get_subnet_declerations: Content to be parsed was not provided"}" - local -i OPEN_BRACKETS=0 - local -i CLOSE_BRACKETS=0 - local CAN_READ=0 + local -a SECTIONS - while read line; do - if [[ "$line" =~ ^$SUBNET_BEGIN_REGEX.*$ ]]; then - CAN_READ=1 - fi - done <<< "$CONTENT" + get_sections $SUBNET_BEGIN_REGEX SECTIONS "$CONTENT" + + local IP_REGEX='[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' + + for i in ${!SECTIONS[@]}; do + SECTION_CONTENT="${SECTIONS[$i]}" + SECTION_NAME=`grep -Eo $IP_REGEX <<< "$SECTION_CONTENT" | head -n 1` + HASH_TABLE["$SECTION_NAME"]="${SECTION_CONTENT}" + done }