#!/usr/bin/env bash # Sync_pihole_lan.sh synchronise the /etc/pihole/lan.list file with your Subdomains # v3.0 italic="\033[3m" underline="\033[4m" ita_under="\033[3;4m" bgd="\033[1;4;31m" red="\033[1;31m" bold="\033[1m" bold_ita="\033[1;3m" box="\033[1;41m" redbold="\033[1;31m" redbox="\033[1;41m" green="\033[0;32m" greenbold="\033[1;32m" reset="\033[0m" absent=() delete=() domain=".photos-nas.ovh" not_required=("drive" "files" "gitea" "home-assistant" "homebridge" "portainer" "tunes" "wg" "yatch") not_used=("ds916" "musiiic" "notif") pihole1=192.168.2.116 pihole2=192.168.2.216 echo -e "${greenbold}Sync_pihole_lan.sh synchronise the /etc/pihole/lan.list file with your Subdomains ...${reset}\n" cat < /dev/null > /dev/tcp/1.1.1.1/53 if [[ $? -ne 0 ]]; then echo -e "\n${red}No Internet connection !${reset}" echo -e "Exit !" exit 1 fi echo -e "${bold}Backup ${italic}lan.list...${reset}" # Sauvegarde du /etc/pihole/lan.list de chaque pihole vers lan.list.1 et lan.list.2 ping -q -c1 "$pihole1" &>/dev/null && ssh -p51322 root@"$pihole1" "cat /etc/pihole/lan.list" > lan.list.1 || echo -e "\n${red}Pihole1 is unreachable !'${reset}" ping -q -c1 "$pihole2" &>/dev/null && ssh -p51522 root@"$pihole2" "cat /etc/pihole/lan.list" > lan.list.2 || echo -e "${red}Pihole2 is unreachable !'${reset}" # Si un lan.list.bak existe, on l'archive if [ -f lan.list.bak ]; then cp lan.list.bak /tmp/lan.list bzip2 /tmp/lan.list # lan.list.bz2 mv /tmp/lan.list.bz2 . fi # On backup le lan.list vers lan.list.bak (sinon lan.list.1 devient lan.list) if [ -f lan.list ]; then cp lan.list lan.list.bak elif [ -f lan.list.1 ]; then cp lan.list.1 lan.list elif [ -f lan.list.2 ]; then cp lan.list.2 lan.list else echo "Error ! No lan.list file !" exit fi nb_dynhost=$(awk 'END { print NR }' lan.list) echo -e "\n$nb_dynhost dynhost found in lan.list !" # Suppression des 'not_required' et 'not_used' dans le lan.list echo -e "${bold}\nFind extra dynhost in ${italic}lan.list...${reset}" while IFS= read -r line; do ndd=$(echo "${line}" | awk '{print $2}' | awk -F"." '{print $1}') if [[ " ${not_required[*]} " =~ " $ndd " ]] || [[ " ${not_used[*]} " =~ " $ndd " ]]; then delete+=(${ndd}) fi done < lan.list if [ ${#delete[@]} -ge 1 ]; then echo -e "${red}To delete: ${delete[@]}${reset}" b=$(echo -e "Do you want to delete ${#delete[@]} extra dynhost in ${italic}lan.list${reset} ? (y/n)") read -p "$b" choice if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then for val in ${!delete[@]} do value="${delete[$val]}$domain" value=$(echo "$value" | sed 's/\./\\\./g') # -i '' for sed BSD ; -i for sed GNU sed -i '' "/$value/d" lan.list done nb_dynhost=$(awk 'END { print NR }' lan.list) echo "$nb_dynhost dynhost found in lan.list !" fi else echo "None" fi # Ajout des DynHOST de OVH (Zones DNS) dans les 2 lan.list (pihole1 & pihole2) # Zones DNS -> photos-nas.ovh -> Historique des zones echo -e "${bold}\nFind missing dynhost in ${italic}lan.list...${reset}" #dynhost_list=$(cat photos-nas.ovh_dns_data.txt | grep "60 IN A" | sed '1d') dynhost_list=$(cat subdomains.photos-nas.ovh.txt) while IFS= read -r line; do dynhost=$(echo "${line}" | awk '{print $1}') grep -q $dynhost lan.list if [ $? != 0 ]; then if [[ ! " ${not_required[*]} " =~ " $dynhost " ]] && [[ ! " ${not_used[*]} " =~ " $dynhost " ]]; then absent+=(${dynhost}) fi fi done <<< "$dynhost_list" if [ ${#absent[@]} -ge 1 ]; then echo -e "${green}Not required: ${not_required[@]}${reset}" echo -e "${italic}Not used: ${not_used[@]}${reset}" echo -e "${red}Missing: ${absent[@]}${reset}" # On supprime /tmp/temp_file.list [ -f /tmp/temp_file.list ] && rm /tmp/temp_file.list # On crée un /tmp/temp_file.list avec les sous-domaines manquants for val in ${absent[@]} do echo -e "192.168.2.57 ${val}.photos-nas.ovh ${val}" >> /tmp/temp_file.list done echo -e "\n${bold}Update ${italic}lan.list...${reset}" cat /tmp/temp_file.list # On ajoute le /tmp/temp_file.list au lan.list cat /tmp/temp_file.list >> lan.list # On trie le lan.list cat lan.list | sort -k2 > lan.list.sorted mv lan.list.sorted lan.list else echo -e "${green}No new dynhost !${reset}" fi echo echo -e "${bold}Display ${italic}lan.list. Please verify IP and subdomains.${reset}" cat lan.list nb_dynhost=$(awk 'END { print NR }' lan.list) echo -e "$nb_dynhost dynhost !\n" # Do you want to modify lan.list ? a=$(echo -e "Do you want to edit ${italic}lan.list${reset} ? (y/n)") read -p "$a" choice if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then nano -l lan.list fi # On exporte lan.list vers chaque /etc/pihole/lan.list (pihole1 et pihole2) : << 'COMMENTS' echo "toto" COMMENTS b=$(echo -e "Do you want to export ${italic}lan.list${reset} to ${bold}pihole1${reset} ($pihole1) and ${bold}pihole2${reset} ($pihole2) ? (y/n)") read -p "$b" choice if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then if ping -q -c1 "$pihole1" &>/dev/null; then echo "Export to pihole1..." cat lan.list | ssh -p51322 root@"$pihole1" 'cat > /etc/pihole/lan.list' [ $? != 0 ] && echo -e "${red}Error during transfer to pihole1 ($pihole1)${reset}" || echo -e "${green}Ok${reset}" echo "Restarting dns on pihole1..." ssh -p51322 root@"$pihole1" 'pihole restartdns' [ $? != 0 ] && echo -e "${red}Error during restarting dns on pihole1 ($pihole1)${reset}" || echo -e "${green}Ok${reset}" else echo -e "\n${red}Pihole1 is unreachable !'${reset}" fi if ping -q -c1 "$pihole2" &>/dev/null; then echo "Export to pihole2..." cat lan.list | ssh -p51522 root@"$pihole2" 'cat > /etc/pihole/lan.list' [ $? != 0 ] && echo -e "${red}Error during transfer to pihole2 ($pihole2)${reset}" || echo -e "${green}Ok${reset}" echo "Restarting dns on pihole2..." ssh -p51522 root@"$pihole2" 'pihole restartdns' [ $? != 0 ] && echo -e "${red}Error during restarting dns on pihole2 ($pihole2)${reset}" || echo -e "${green}Ok${reset}" else echo -e "n${red}Pihole2 is unreachable !'${reset}" fi else echo -e "OK, let's continue..." fi # On supprime les fichiers temporaires [ -f /tmp/temp_file.list ] && rm /tmp/temp_file.list [ -f lan.list.sorted ] && rm lan.list.sorted # https://www.nas-forum.com/forum/topic/80160-multiples-dynhost-ovh/#comment-1319505612