Reworked it

This commit is contained in:
Tristan Ancelet 2023-11-12 17:08:54 -06:00
parent 78af1a1961
commit 77d8e3dd7c
3 changed files with 37 additions and 59 deletions

16
utils/hosts.sh Executable file
View File

@ -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
}

View File

@ -1,53 +1,18 @@
#!/bin/bash #!/bin/bash
# BEGIN: Variables
DHCPD_CONF_CONTENTS="$(<${CONFIG_FILE:?"DHCPD_CONFIG was not set"})"
# END: Variables
# BEGIN: Functions
get_shared_network_declarations () { get_shared_network_declarations () {
local CURRENT_BUFFER="" local -n HASH_TABLE="${1:?"get_shared_network_declarations: Hash table variable not passed into function"}"
local CURRENT_SHARED_NETWORK_NAME="" local CONTENT="${2:?"get_shared_network_declarations: Content was not provided"}"
local -i OPEN_BRACKETS=0 local -a SECTIONS
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 local SECTION_CONTENT SECTION_NAME
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 get_sections "$SHARED_NETWORK_BEGIN_REGEX" SECTIONS "$CONTENT"
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" 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

25
utils/subnets.sh Normal file → Executable file
View File

@ -1,20 +1,17 @@
#!/bin/bash #!/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 () { get_subnet_declerations () {
local SECTION_CONTENT SECTION_NAME
local -n HASH_TABLE="${1:?"get_subnet_declerations: Hash table was not provided"}" 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 CONTENT="${2:?"get_subnet_declerations: Content to be parsed was not provided"}"
local -i OPEN_BRACKETS=0 local -a SECTIONS
local -i CLOSE_BRACKETS=0
local CAN_READ=0
while read line; do get_sections $SUBNET_BEGIN_REGEX SECTIONS "$CONTENT"
if [[ "$line" =~ ^$SUBNET_BEGIN_REGEX.*$ ]]; then
CAN_READ=1 local IP_REGEX='[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'
fi
done <<< "$CONTENT" 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
} }