diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ecc51b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +* +!README.md +!.gitignore +!utils +!utils/* + diff --git a/utils/myread.sh b/utils/myread.sh new file mode 100644 index 0000000..82c5b09 --- /dev/null +++ b/utils/myread.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +get_sections() { + local START_REGEX="${1:?"get_sections: START_REGEX was not provided"}" + local -n CONTAINER="${2:?"get_sections: Has CONTAINER variable not passed into function"}" + local CONTENT="${3:?"get_sections: Content was not provided"}" + + local CURRENT_BUFFER="" + local -i OPEN_BRACKETS=0 + local -i CLOSE_BRACKETS=0 + local CAN_READ=0 + + while read line; do + if [[ "$line" =~ ^$START_REGEX.*$ ]]; then + CAN_READ=1 + fi + + if [[ $CAN_READ -eq 1 ]]; then + CURRENT_BUFFER+="$line\n" + [[ "$line" == *'{'* ]] && ((OPEN_BRACKETS++)) + [[ "$line" == *'}'* ]] && ((CLOSE_BRACKETS++)) + if [[ $OPEN_BRACKETS -eq $CLOSE_BRACKETS ]]; then + CONTAINER+=( "$( echo -e "$CURRENT_BUFFER" )" ) + CURRENT_BUFFER="" + CAN_READ=0 + OPEN_BRACKETS=0 + CLOSE_BRACKETS=0 + fi + fi + + done <<< "$CONTENT" +} + diff --git a/utils/shared-networks.sh b/utils/shared-networks.sh new file mode 100755 index 0000000..3467458 --- /dev/null +++ b/utils/shared-networks.sh @@ -0,0 +1,53 @@ +#!/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"}" + + 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 + + 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 + + done <<< "$DHCPD_CONF_CONTENTS" +} + +# 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 new file mode 100644 index 0000000..7ac9c1e --- /dev/null +++ b/utils/subnets.sh @@ -0,0 +1,20 @@ +#!/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 -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 + + while read line; do + if [[ "$line" =~ ^$SUBNET_BEGIN_REGEX.*$ ]]; then + CAN_READ=1 + fi + done <<< "$CONTENT" +}