Created
January 18, 2025 11:05
-
-
Save jult/7f4b141710710b1a0da4591500c4389d to your computer and use it in GitHub Desktop.
convert pihole dnsmasq static dhcp hosts to isc-kea kea dhcp-server json format
This file contains hidden or 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
#!/bin/bash | |
# Check if jq is installed | |
if ! command -v jq &> /dev/null; then | |
echo "Error: jq is not installed. Please install jq using 'sudo apt install jq'" | |
exit 1 | |
fi | |
# Source the dnsmasq static DHCP list | |
static_leases=$(cat /etc/dnsmasq.d/04-pihole-static-dhcp.conf) | |
# Define output file | |
output_file="/etc/kea/kea-dhcp4.conf" | |
# Start the JSON structure for reservations | |
echo '{ "reservations": [' > "$output_file" | |
# Process each line in the dnsmasq static DHCP list | |
while IFS= read -r line; do | |
# Extract the MAC address, IP address, and hostname from the line | |
mac_address=$(echo "$line" | cut -d',' -f1 | cut -d'=' -f2 | xargs) | |
ip_address=$(echo "$line" | cut -d',' -f2 | xargs) | |
hostname=$(echo "$line" | cut -d',' -f3- | xargs) | |
# Create a JSON object for the static lease | |
json_object="{ \"hw-address\": \"$mac_address\", \"ip-address\": \"$ip_address\", \"hostname\": \"$hostname\" }" | |
# Append the JSON object to the output file with a newline | |
echo " $json_object," >> "$output_file" | |
done <<< "$static_leases" | |
# Remove the trailing comma from the last entry | |
sed -i '$ s/,$//' "$output_file" | |
# Close the JSON array | |
echo "]}" >> "$output_file" | |
echo "Kea DHCP4 configuration file ($output_file) updated successfully." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment