From d823af896a2f43d2aa7e8a55a0f1802a03ddb605 Mon Sep 17 00:00:00 2001 From: ryt51V Date: Mon, 29 Feb 2016 21:57:44 +0000 Subject: [PATCH] Install script now saves interface and IPv4 address to files in /etc/pihole rather than /tmp. gravity.sh, blacklist.sh, whitelist.sh read from these files first. If they cannot find them then they warn the user and detect and use the most sensible interface / IPv4 address. gravity.sh no longer deletes the IPv4 file. --- advanced/Scripts/blacklist.sh | 28 +++++++++++++++++++++++----- advanced/Scripts/whitelist.sh | 28 +++++++++++++++++++++++----- automated install/basic-install.sh | 10 ++++++---- gravity.sh | 26 +++++++++++++++++++------- 4 files changed, 71 insertions(+), 21 deletions(-) diff --git a/advanced/Scripts/blacklist.sh b/advanced/Scripts/blacklist.sh index 70b8131a..d3ca9840 100644 --- a/advanced/Scripts/blacklist.sh +++ b/advanced/Scripts/blacklist.sh @@ -34,13 +34,31 @@ domList=() domToRemoveList=() -piholeIPfile=/tmp/piholeIP +piholeINTfile=/etc/pihole/piholeINT +piholeIPfile=/etc/pihole/piholeIP piholeIPv6file=/etc/pihole/.useIPv6 -# Otherwise, the IP address can be taken directly from the machine, which will happen when the script is run by the user and not the installation script -IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}') -piholeIPCIDR=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | awk 'END {print}') -piholeIP=${piholeIPCIDR%/*} +# Know which interface we are using. +if [[ -f $piholeINTfile ]]; then + # This file should normally exist - it was saved as part of the install. + IPv4dev=$(cat $piholeINTfile) +else + # If it doesn't, we err on the side of working with the majority of setups and detect the most likely interface. + IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}') + echo "::: Warning: ${piholeINTfile} is missing. Using interface ${IPv4dev}." +fi + +# Know which IPv4 address we are using. +if [[ -f $piholeIPfile ]];then + # This file should normally exist - it was saved as part of the install. + piholeIP=$(cat $piholeIPfile) +else + # If it doesn't, we err on the side of working with the majority of setups and detect the most likely IPv4 address, + # which is the first one we find belonging to the given interface. + piholeIPCIDR=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | head -n 1) + piholeIP=${piholeIPCIDR%/*} + echo "::: Warning: ${piholeIPfile} is missing. Using IPv4 address ${piholeIP}." +fi modifyHost=false diff --git a/advanced/Scripts/whitelist.sh b/advanced/Scripts/whitelist.sh index 853c3b79..b1833486 100755 --- a/advanced/Scripts/whitelist.sh +++ b/advanced/Scripts/whitelist.sh @@ -33,13 +33,31 @@ versbose=true domList=() domToRemoveList=() -piholeIPfile=/tmp/piholeIP +piholeINTfile=/etc/pihole/piholeINT +piholeIPfile=/etc/pihole/piholeIP piholeIPv6file=/etc/pihole/.useIPv6 -# Otherwise, the IP address can be taken directly from the machine, which will happen when the script is run by the user and not the installation script -IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}') -piholeIPCIDR=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | awk 'END {print}') -piholeIP=${piholeIPCIDR%/*} +# Know which interface we are using. +if [[ -f $piholeINTfile ]]; then + # This file should normally exist - it was saved as part of the install. + IPv4dev=$(cat $piholeINTfile) +else + # If it doesn't, we err on the side of working with the majority of setups and detect the most likely interface. + IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}') + echo "::: Warning: ${piholeINTfile} is missing. Using interface ${IPv4dev}." +fi + +# Know which IPv4 address we are using. +if [[ -f $piholeIPfile ]];then + # This file should normally exist - it was saved as part of the install. + piholeIP=$(cat $piholeIPfile) +else + # If it doesn't, we err on the side of working with the majority of setups and detect the most likely IPv4 address, + # which is the first one we find belonging to the given interface. + piholeIPCIDR=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | head -n 1) + piholeIP=${piholeIPCIDR%/*} + echo "::: Warning: ${piholeIPfile} is missing. Using IPv4 address ${piholeIP}." +fi modifyHost=false diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index 10974762..f80a16fe 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -152,7 +152,6 @@ chooseInterface() { do piholeInterface=$desiredInterface echo "::: Using interface: $piholeInterface" - echo ${piholeInterface} > /tmp/piholeINT done else echo "::: Cancel selected, exiting...." @@ -219,6 +218,9 @@ use4andor6() { Gateway: $IPv4gw" $r $c) then echo "::: Leaving IPv4 settings as is." + # Saving the IP and interface to a file for future use by other scripts (gravity.sh, whitelist.sh, etc.) + echo ${IPv4addr%/*} > /etc/pihole/piholeIP + echo $piholeInterface > /etc/pihole/piholeINT else getStaticIPv4Settings setStaticIPv4 @@ -284,9 +286,9 @@ getStaticIPv4Settings() { IP address: $IPv4addr Gateway: $IPv4gw" $r $c)then # If the settings are correct, then we need to set the piholeIP - # Saving it to a temporary file us to retrieve it later when we run the gravity.sh script - echo ${IPv4addr%/*} > /tmp/piholeIP - echo $piholeInterface > /tmp/piholeINT + # Saving the IP and interface to a file for future use by other scripts (gravity.sh, whitelist.sh, etc.) + echo ${IPv4addr%/*} > /etc/pihole/piholeIP + echo $piholeInterface > /etc/pihole/piholeINT # After that's done, the loop ends and we move on ipSettingsCorrect=True else diff --git a/gravity.sh b/gravity.sh index 321aec27..1b193a55 100755 --- a/gravity.sh +++ b/gravity.sh @@ -26,7 +26,8 @@ else fi fi -piholeIPfile=/tmp/piholeIP +piholeINTfile=/etc/pihole/piholeINT +piholeIPfile=/etc/pihole/piholeIP piholeIPv6file=/etc/pihole/.useIPv6 adListFile=/etc/pihole/adlists.list @@ -34,15 +35,26 @@ adListDefault=/etc/pihole/adlists.default whitelistScript=/usr/local/bin/whitelist.sh blacklistScript=/usr/local/bin/blacklist.sh -if [[ -f $piholeIPfile ]];then - # If the file exists, it means it was exported from the installation script and we should use that value instead of detecting it in this script - piholeIP=$(cat $piholeIPfile) - rm $piholeIPfile +# Know which interface we are using. +if [[ -f $piholeINTfile ]]; then + # This file should normally exist - it was saved as part of the install. + IPv4dev=$(cat $piholeINTfile) else - # Otherwise, the IP address can be taken directly from the machine, which will happen when the script is run by the user and not the installation script + # If it doesn't, we err on the side of working with the majority of setups and detect the most likely interface. IPv4dev=$(ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}') - piholeIPCIDR=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | awk 'END {print}') + echo "::: Warning: ${piholeINTfile} is missing. Using interface ${IPv4dev}." +fi + +# Know which IPv4 address we are using. +if [[ -f $piholeIPfile ]];then + # This file should normally exist - it was saved as part of the install. + piholeIP=$(cat $piholeIPfile) +else + # If it doesn't, we err on the side of working with the majority of setups and detect the most likely IPv4 address, + # which is the first one we find belonging to the given interface. + piholeIPCIDR=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}' | head -n 1) piholeIP=${piholeIPCIDR%/*} + echo "::: Warning: ${piholeIPfile} is missing. Using IPv4 address ${piholeIP}." fi if [[ -f $piholeIPv6file ]];then