From 29ae66c6db1d8748ba54fc79a9ec12dc56211afb Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Wed, 25 Nov 2015 13:43:59 -0800 Subject: [PATCH 1/4] Check IP info for outgoing interface --- automated install/basic-install.sh | 46 ++++++++++++------------------ 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index f0a7d13..2609973 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # Pi-hole: A black hole for Internet advertisements # by Jacob Salmela # Network-wide ad blocking via your Raspberry Pi @@ -24,9 +24,11 @@ columns=$(stty -a | tr \; \\012 | egrep 'columns' | cut -d' ' -f3) r=$(( rows / 2 )) c=$(( columns / 2 )) -IPv4addr=$(ip -4 addr show | awk '{match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/); ip = substr($0,RSTART,RLENGTH); print ip}' | sed '/^\s*$/d' | grep -v "127.0.0.1") -IPv4mask=$(ifconfig | awk -F':' '/inet addr/ && !/127.0.0.1/ {print $4}') -IPv4gw=$(ip route show | awk '/default\ via/ {print $3}') +# Find IP used to route to outside world +IPv4info=$(ip route get 8.8.8.8) +IPv4dev=$(echo $IPv4info| awk '{print $5}') +IPv4addr=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}') +IPv4gw=$(echo $IPv4info | awk '{print $3}') # IPv6 support to be added later #IPv6eui64=$(ip addr show | awk '/scope\ global/ && /ff:fe/ {print $2}' | cut -d'/' -f1) @@ -117,7 +119,6 @@ getStaticIPv4Settings() if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Do you want to use your current network settings as a static address? IP address: $IPv4addr - Subnet mask: $IPv4mask Gateway: $IPv4gw" $r $c) then # If they choose yes, let the user know that the IP address will not be available via DHCP and may cause a conflict. whiptail --msgbox --backtitle "IP information" --title "FYI: IP Conflict" "It is possible your router could still try to assign this IP to a device, which would cause a conflict. But in most cases the router is smart enough to not do that. @@ -138,22 +139,17 @@ else IPv4addr=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 address" --inputbox "Enter your desired IPv4 address" $r $c $IPv4addr 3>&1 1>&2 2>&3) if [[ $? = 0 ]];then echo "Your static IPv4 address: $IPv4addr" - # Ask for the subnet mask - IPv4mask=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 netmask" --inputbox "Enter your desired IPv4 subnet mask" $r $c $IPv4mask 3>&1 1>&2 2>&3) - if [[ $? = 0 ]];then - echo "Your static IPv4 netmask: $IPv4mask" - # Ask for the gateway + # Ask for the gateway IPv4gw=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 gateway (router)" --inputbox "Enter your desired IPv4 default gateway" $r $c $IPv4gw 3>&1 1>&2 2>&3) if [[ $? = 0 ]];then echo "Your static IPv4 gateway: $IPv4gw" # Give the user a chance to review their settings before moving on if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Are these settings correct? IP address: $IPv4addr - Subnet mask: $IPv4mask 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 ${IPv4addr%/*} > /tmp/piholeIP # After that's done, the loop ends and we move on ipSettingsCorrect=True else @@ -167,12 +163,6 @@ else exit fi else - # Cancelling subnet mask settings window - ipSettingsCorrect=False - echo "User canceled." - exit - fi - else # Cancelling IPv4 settings window ipSettingsCorrect=False echo "User canceled." @@ -188,7 +178,7 @@ setStaticIPv4() { # Append these lines to /etc/dhcpcd.conf to enable a static IP echo "interface $piholeInterface -static ip_address=$IPv4addr/24 +static ip_address=$IPv4addr static routers=$IPv4gw static domain_name_servers=$IPv4gw" | sudo tee -a $dhcpcdFile >/dev/null } @@ -224,15 +214,15 @@ sudo rm /var/www/master.zip 2>/dev/null sudo touch /var/log/pihole.log sudo chmod 644 /var/log/pihole.log sudo chown dnsmasq:root /var/log/pihole.log -sudo curl -o /usr/local/bin/gravity.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/gravity.sh -sudo curl -o /usr/local/bin/chronometer.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/chronometer.sh -sudo curl -o /usr/local/bin/whitelist.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/whitelist.sh -sudo curl -o /usr/local/bin/piholeLogFlush.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/piholeLogFlush.sh -sudo chmod 755 /usr/local/bin/gravity.sh -sudo chmod 755 /usr/local/bin/chronometer.sh -sudo chmod 755 /usr/local/bin/whitelist.sh -sudo chmod 755 /usr/local/bin/piholeLogFlush.sh -sudo /usr/local/bin/gravity.sh +sudo curl -o /usr/bin/gravity.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/gravity.sh +sudo curl -o /usr/bin/chronometer.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/chronometer.sh +sudo curl -o /usr/bin/whitelist.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/whitelist.sh +sudo curl -o /usr/bin/piholeLogFlush.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/piholeLogFlush.sh +sudo chmod 755 /usr/bin/gravity.sh +sudo chmod 755 /usr/bin/chronometer.sh +sudo chmod 755 /usr/bin/whitelist.sh +sudo chmod 755 /usr/bin/piholeLogFlush.sh +sudo /usr/bin/gravity.sh } ######## SCRIPT ############ From 6e13134328c779375b74b2a2f6d00ee39e67f578 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Wed, 25 Nov 2015 13:51:11 -0800 Subject: [PATCH 2/4] Revert FHS change until merged --- automated install/basic-install.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index 2609973..f2a98fb 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -214,15 +214,15 @@ sudo rm /var/www/master.zip 2>/dev/null sudo touch /var/log/pihole.log sudo chmod 644 /var/log/pihole.log sudo chown dnsmasq:root /var/log/pihole.log -sudo curl -o /usr/bin/gravity.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/gravity.sh -sudo curl -o /usr/bin/chronometer.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/chronometer.sh -sudo curl -o /usr/bin/whitelist.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/whitelist.sh -sudo curl -o /usr/bin/piholeLogFlush.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/piholeLogFlush.sh -sudo chmod 755 /usr/bin/gravity.sh -sudo chmod 755 /usr/bin/chronometer.sh -sudo chmod 755 /usr/bin/whitelist.sh -sudo chmod 755 /usr/bin/piholeLogFlush.sh -sudo /usr/bin/gravity.sh +sudo curl -o /usr/local/bin/gravity.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/gravity.sh +sudo curl -o /usr/local/bin/chronometer.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/chronometer.sh +sudo curl -o /usr/local/bin/whitelist.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/whitelist.sh +sudo curl -o /usr/local/bin/piholeLogFlush.sh https://raw.githubusercontent.com/jacobsalmela/pi-hole/master/advanced/Scripts/piholeLogFlush.sh +sudo chmod 755 /usr/local/bin/gravity.sh +sudo chmod 755 /usr/local/bin/chronometer.sh +sudo chmod 755 /usr/local/bin/whitelist.sh +sudo chmod 755 /usr/local/bin/piholeLogFlush.sh +sudo /usr/local/bin/gravity.sh } ######## SCRIPT ############ From 21ee63b7fb77da949eca5aca710afade971a0236 Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Wed, 25 Nov 2015 14:08:13 -0800 Subject: [PATCH 3/4] Get IP info from outgoing interface Use IP to get interface information from outgoing interface. Use CIDR notation to make things easier. --- automated install/basic-install.sh | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index f0a7d13..f2a98fb 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # Pi-hole: A black hole for Internet advertisements # by Jacob Salmela # Network-wide ad blocking via your Raspberry Pi @@ -24,9 +24,11 @@ columns=$(stty -a | tr \; \\012 | egrep 'columns' | cut -d' ' -f3) r=$(( rows / 2 )) c=$(( columns / 2 )) -IPv4addr=$(ip -4 addr show | awk '{match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/); ip = substr($0,RSTART,RLENGTH); print ip}' | sed '/^\s*$/d' | grep -v "127.0.0.1") -IPv4mask=$(ifconfig | awk -F':' '/inet addr/ && !/127.0.0.1/ {print $4}') -IPv4gw=$(ip route show | awk '/default\ via/ {print $3}') +# Find IP used to route to outside world +IPv4info=$(ip route get 8.8.8.8) +IPv4dev=$(echo $IPv4info| awk '{print $5}') +IPv4addr=$(ip -o -f inet addr show dev $IPv4dev | awk '{print $4}') +IPv4gw=$(echo $IPv4info | awk '{print $3}') # IPv6 support to be added later #IPv6eui64=$(ip addr show | awk '/scope\ global/ && /ff:fe/ {print $2}' | cut -d'/' -f1) @@ -117,7 +119,6 @@ getStaticIPv4Settings() if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Do you want to use your current network settings as a static address? IP address: $IPv4addr - Subnet mask: $IPv4mask Gateway: $IPv4gw" $r $c) then # If they choose yes, let the user know that the IP address will not be available via DHCP and may cause a conflict. whiptail --msgbox --backtitle "IP information" --title "FYI: IP Conflict" "It is possible your router could still try to assign this IP to a device, which would cause a conflict. But in most cases the router is smart enough to not do that. @@ -138,22 +139,17 @@ else IPv4addr=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 address" --inputbox "Enter your desired IPv4 address" $r $c $IPv4addr 3>&1 1>&2 2>&3) if [[ $? = 0 ]];then echo "Your static IPv4 address: $IPv4addr" - # Ask for the subnet mask - IPv4mask=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 netmask" --inputbox "Enter your desired IPv4 subnet mask" $r $c $IPv4mask 3>&1 1>&2 2>&3) - if [[ $? = 0 ]];then - echo "Your static IPv4 netmask: $IPv4mask" - # Ask for the gateway + # Ask for the gateway IPv4gw=$(whiptail --backtitle "Calibrating network interface" --title "IPv4 gateway (router)" --inputbox "Enter your desired IPv4 default gateway" $r $c $IPv4gw 3>&1 1>&2 2>&3) if [[ $? = 0 ]];then echo "Your static IPv4 gateway: $IPv4gw" # Give the user a chance to review their settings before moving on if (whiptail --backtitle "Calibrating network interface" --title "Static IP Address" --yesno "Are these settings correct? IP address: $IPv4addr - Subnet mask: $IPv4mask 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 ${IPv4addr%/*} > /tmp/piholeIP # After that's done, the loop ends and we move on ipSettingsCorrect=True else @@ -167,12 +163,6 @@ else exit fi else - # Cancelling subnet mask settings window - ipSettingsCorrect=False - echo "User canceled." - exit - fi - else # Cancelling IPv4 settings window ipSettingsCorrect=False echo "User canceled." @@ -188,7 +178,7 @@ setStaticIPv4() { # Append these lines to /etc/dhcpcd.conf to enable a static IP echo "interface $piholeInterface -static ip_address=$IPv4addr/24 +static ip_address=$IPv4addr static routers=$IPv4gw static domain_name_servers=$IPv4gw" | sudo tee -a $dhcpcdFile >/dev/null } From b011fe28d9ba095f6f6a057dc3a08bc0749f305b Mon Sep 17 00:00:00 2001 From: Dan Schaper Date: Wed, 25 Nov 2015 14:34:06 -0800 Subject: [PATCH 4/4] No longer need a reboot --- automated install/basic-install.sh | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index f2a98fb..47c402f 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -128,8 +128,6 @@ if (whiptail --backtitle "Calibrating network interface" --title "Static IP Addr It is also possible to use a DHCP reservation, but if you are going to do that, you might as well set a static address." $r $c # Nothing else to do since the variables are already set above else - # Since a custom address will be used, restart at the end of the script to apply the new changes - rebootNeeded=true # Otherwise, we need to ask the user to input their desired settings. # Start by getting the IPv4 address (pre-filling it with info gathered from DHCP) # Start a loop to let the user enter their information with the chance to go back and edit it if necessary @@ -180,7 +178,8 @@ setStaticIPv4() echo "interface $piholeInterface static ip_address=$IPv4addr static routers=$IPv4gw -static domain_name_servers=$IPv4gw" | sudo tee -a $dhcpcdFile >/dev/null +static domain_name_servers=$IPv4gw" | sudo tee -a $dhcpcdFile >/dev/null +sudo ip addr replace dev $piholeInterface $IPv4addr } installPihole() @@ -274,12 +273,5 @@ If you didn't use DHCP settings as your new static address, the Pi will restart The install log is in /etc/pihole." $r $c -# If a custom address was set, restart -if [[ "$rebootNeeded" = true ]];then - # Restart to apply the new static IP address - sudo reboot -else - # If not, just start the services since the address will stay the same - sudo service dnsmasq start - sudo service lighttpd start -fi +sudo service dnsmasq start +sudo service lighttpd start