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
# 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

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

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