From 9f6b247a4a808652b5ce7212e74ca79b76394b02 Mon Sep 17 00:00:00 2001 From: SDGDen Date: Mon, 9 Mar 2026 10:06:29 +0100 Subject: [PATCH] fixed dhcp... again. i hope. --- deploy.sh | 54 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/deploy.sh b/deploy.sh index a805b0f..db19388 100644 --- a/deploy.sh +++ b/deploy.sh @@ -586,12 +586,19 @@ fi # Enable DHCP on PiHole for options 2 and 3 if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; then echo "Configuring DHCP on PiHole..." + echo "Gathering network information..." + # Get network information gateway=$(ip route | awk '/default/ {print $3}') interface=$(ip route | awk '/default/ {print $5}') netmask=$(ifconfig $interface | awk '/netmask/ {print $4}') localip=$(hostname -I | awk '{print $1}') + echo " Gateway: $gateway" + echo " Interface: $interface" + echo " Netmask: $netmask" + echo " Local IP: $localip" + # Function to convert IP to binary string ip_to_bin() { local ip=$1 @@ -615,11 +622,15 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t local bin_ip=$1 local num=$2 # Convert binary IP to decimal - local dec_ip=$((2#${bin_ip:0:8}00000000 + 2#00000000${bin_ip:8:8}0000000 + 2#0000000000000000${bin_ip:16:8}0000000 + 2#000000000000000000000000${bin_ip:24:8})) + local dec_ip=$((2#${bin_ip:0:8} * 256**3 + 2#${bin_ip:8:8} * 256**2 + 2#${bin_ip:16:8} * 256 + 2#${bin_ip:24:8})) # Add the number dec_ip=$((dec_ip + num)) # Convert back to binary IP - local new_bin_ip=$(printf "%032d" $(echo "obase=2; $dec_ip" | bc)) + local new_bin_ip=$(printf "%08d%08d%08d%08d" \ + "$(echo "obase=2; ($dec_ip >> 24) & 255" | bc)" \ + "$(echo "obase=2; ($dec_ip >> 16) & 255" | bc)" \ + "$(echo "obase=2; ($dec_ip >> 8) & 255" | bc)" \ + "$(echo "obase=2; $dec_ip & 255" | bc)") echo "$new_bin_ip" } @@ -651,11 +662,14 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t network_address=$(bin_to_ip "$network_bin") broadcast_address=$(bin_to_ip "$broadcast_bin") + echo " Network Address: $network_address" + echo " Broadcast Address: $broadcast_address" + # Calculate DHCP range using binary operations # Start DHCP 10 IPs after the gateway (but not before network+1) dhcp_start_bin=$(add_to_bin_ip "$gateway_bin" 10) - network_dec=$(printf "%d" "$(echo "ibase=2; ${network_bin}" | bc)") - dhcp_start_dec=$(printf "%d" "$(echo "ibase=2; ${dhcp_start_bin}" | bc)") + network_dec=$((2#${network_bin:0:8} * 256**3 + 2#${network_bin:8:8} * 256**2 + 2#${network_bin:16:8} * 256 + 2#${network_bin:24:8})) + dhcp_start_dec=$((2#${dhcp_start_bin:0:8} * 256**3 + 2#${dhcp_start_bin:8:8} * 256**2 + 2#${dhcp_start_bin:16:8} * 256 + 2#${dhcp_start_bin:24:8})) if [ "$dhcp_start_dec" -le "$network_dec" ]; then dhcp_start_bin=$(add_to_bin_ip "$network_bin" 1) @@ -663,17 +677,17 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t # End DHCP 100 IPs after start (but not after broadcast-1) dhcp_end_bin=$(add_to_bin_ip "$dhcp_start_bin" 100) - broadcast_dec=$(printf "%d" "$(echo "ibase=2; ${broadcast_bin}" | bc)") - dhcp_end_dec=$(printf "%d" "$(echo "ibase=2; ${dhcp_end_bin}" | bc)") + broadcast_dec=$((2#${broadcast_bin:0:8} * 256**3 + 2#${broadcast_bin:8:8} * 256**2 + 2#${broadcast_bin:16:8} * 256 + 2#${broadcast_bin:24:8})) + dhcp_end_dec=$((2#${dhcp_end_bin:0:8} * 256**3 + 2#${dhcp_end_bin:8:8} * 256**2 + 2#${dhcp_end_bin:16:8} * 256 + 2#${dhcp_end_bin:24:8})) if [ "$dhcp_end_dec" -ge "$broadcast_dec" ]; then dhcp_end_bin=$(add_to_bin_ip "$broadcast_bin" -1) fi # Ensure we don't include the local IP in the DHCP range - localip_dec=$(printf "%d" "$(echo "ibase=2; ${localip_bin}" | bc)") - dhcp_start_dec=$(printf "%d" "$(echo "ibase=2; ${dhcp_start_bin}" | bc)") - dhcp_end_dec=$(printf "%d" "$(echo "ibase=2; ${dhcp_end_bin}" | bc)") + localip_dec=$((2#${localip_bin:0:8} * 256**3 + 2#${localip_bin:8:8} * 256**2 + 2#${localip_bin:16:8} * 256 + 2#${localip_bin:24:8})) + dhcp_start_dec=$((2#${dhcp_start_bin:0:8} * 256**3 + 2#${dhcp_start_bin:8:8} * 256**2 + 2#${dhcp_start_bin:16:8} * 256 + 2#${dhcp_start_bin:24:8})) + dhcp_end_dec=$((2#${dhcp_end_bin:0:8} * 256**3 + 2#${dhcp_end_bin:8:8} * 256**2 + 2#${dhcp_end_bin:16:8} * 256 + 2#${dhcp_end_bin:24:8})) if [ "$localip_dec" -ge "$dhcp_start_dec" ] && [ "$localip_dec" -le "$dhcp_end_dec" ]; then # If local IP is in the range, adjust the range @@ -688,7 +702,20 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t dhcp_start=$(bin_to_ip "$dhcp_start_bin") dhcp_end=$(bin_to_ip "$dhcp_end_bin") + # Validate DHCP range + if [ -z "$dhcp_start" ] || [ -z "$dhcp_end" ]; then + echo "Error: Invalid DHCP range calculated. DHCP start or end is empty. Aborting." + exit 1 + fi + + echo "--- DHCP Configuration Summary ---" + echo " DHCP Range: $dhcp_start to $dhcp_end" + echo " Router: $gateway" + echo " Netmask: $netmask" + echo "----------------------------------" + # Apply the DHCP configuration + echo "Applying DHCP configuration..." docker exec pihole pihole-FTL --config dhcp.active true docker exec pihole pihole-FTL --config dhcp.start "$dhcp_start" docker exec pihole pihole-FTL --config dhcp.end "$dhcp_end" @@ -696,15 +723,10 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t docker exec pihole pihole-FTL --config dhcp.netmask "$netmask" # Restart PiHole to apply changes + echo "Restarting PiHole to apply changes..." docker restart pihole - echo "DHCP configuration applied:" - echo " Network: $network_address" - echo " Broadcast: $broadcast_address" - echo " Range: $dhcp_start to $dhcp_end" - echo " Router: $gateway" - echo " Netmask: $netmask" - echo " Domain: $domain" + echo "DHCP configuration applied successfully." fi # Generate post-install tasks file