-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add netplan config parding sh utility for ubuntu static ip
Signed-off-by: yaacov <[email protected]>
- Loading branch information
Showing
4 changed files
with
191 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/bin/bash | ||
|
||
# Function to extract the interface name and all valid IPv4 addresses from a block of text | ||
parse_interface_block() { | ||
block="$1" | ||
|
||
# Regular expression for matching IPv4 and IPv6 addresses | ||
ip_regex="([0-9]{1,3}\.){3}[0-9]{1,3}(/([0-9]|[1-2][0-9]|3[0-2]))?" | ||
|
||
# Extract the interface name (first word before ":") and take only the first match | ||
interface_name=$(echo "$block" | grep -Eo '^[^:]+' | head -n 1) | ||
|
||
# Find and capture all IP addresses matching the regex in the block | ||
ip_addresses=$(echo "$block" | grep -Eo "$ip_regex" | tr '\n' ' ') | ||
|
||
# Return the interface name and concatenated IP addresses | ||
echo "${interface_name}: ${ip_addresses}" | ||
} | ||
|
||
# Function to parse the netplan file | ||
parse_netplan_file() { | ||
netplan_file="$1" | ||
|
||
# Initialize variables | ||
found_ethernets=0 | ||
interface_indent="" | ||
current_block="" | ||
interfaces_list="" | ||
|
||
# Read the file line by line | ||
while IFS= read -r line; do | ||
# Check if the current line contains "ethernets:" | ||
if [ "$found_ethernets" -eq 0 ] && echo "$line" | grep -q '^[[:space:]]*ethernets:'; then | ||
found_ethernets=1 | ||
continue | ||
fi | ||
|
||
# Once "ethernets:" is found, determine the indentation of the interface block | ||
if [ "$found_ethernets" -eq 1 ] && [ -n "$line" ]; then | ||
interface_indent=$(echo "$line" | sed -e 's/^\([[:space:]]*\).*/\1/' | wc -c) | ||
found_ethernets=2 | ||
fi | ||
|
||
# If we are inside the "ethernets" section, capture each interface block | ||
if [ "$found_ethernets" -eq 2 ]; then | ||
current_indent=$(echo "$line" | sed -e 's/^\([[:space:]]*\).*/\1/' | wc -c) | ||
|
||
# If a new interface is found (same indentation as the interface level) | ||
if [ "$current_indent" -eq "$interface_indent" ]; then | ||
# Parse the previous block and add it to the list | ||
if [ -n "$current_block" ]; then | ||
interfaces_list="$interfaces_list$(parse_interface_block "$current_block") \n" | ||
fi | ||
|
||
# Start a new interface block | ||
current_block="$line" | ||
else | ||
current_block="$current_block\n$line" | ||
fi | ||
|
||
# Stop capturing when encountering a line with less indentation than "interface_indent" | ||
if [ "$current_indent" -lt "$interface_indent" ] && [ -n "$line" ]; then | ||
found_ethernets=0 | ||
fi | ||
fi | ||
done < "$netplan_file" | ||
|
||
# Parse and add the last interface block | ||
if [ -n "$current_block" ]; then | ||
interfaces_list="$interfaces_list$(parse_interface_block "$current_block") \n" | ||
fi | ||
|
||
# Output the parsed interfaces with their IP addresses | ||
printf "$interfaces_list" | ||
} | ||
|
||
# Function to parse all YAML files in a directory using parse_netplan_file | ||
parse_netplan_dir() { | ||
dir="$1" | ||
result="" | ||
|
||
# Iterate over all YAML files in the directory | ||
for yaml_file in "$dir"/*.yaml; do | ||
if [ -f "$yaml_file" ]; then | ||
# Concatenate the output of parse_netplan_file for each YAML file | ||
result=$(printf "%s\n%s" "$result" "$(parse_netplan_file "$yaml_file")") | ||
fi | ||
done | ||
|
||
# Return the concatenated result | ||
echo "$result" | ||
} | ||
|
||
# Call the function to parse the netplan files | ||
parse_netplan_dir "$NETPLAN_DIR" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters