diff --git a/lan.list b/lan.list index e97fc8d..f041aec 100644 --- a/lan.list +++ b/lan.list @@ -8,6 +8,7 @@ 192.168.2.57 docspell.photos-nas.ovh docspell 192.168.2.57 dozzle.photos-nas.ovh dozzle 192.168.2.57 ds923.photos-nas.ovh dsm +192.168.2.57 git.photos-nas.ovh git 192.168.2.57 glances.photos-nas.ovh glances 192.168.2.57 gokapi.photos-nas.ovh gokapi 192.168.2.57 gotify.photos-nas.ovh gotify @@ -19,10 +20,8 @@ 192.168.2.57 menu.photos-nas.ovh heimdall 192.168.2.57 navidrome.photos-nas.ovh navidrome 192.168.2.57 nextcloud.photos-nas.ovh nextcloud -192.168.2.57 notif.photos-nas.ovh nfty 192.168.2.57 paperlessngx.photos-nas.ovh paperlessngx 192.168.2.57 pastebin.photos-nas.ovh pastebin -192.168.2.57 pastefy.photos-nas.ovh pastefy 192.168.2.57 photos.photos-nas.ovh photos 192.168.2.57 pingvin.photos-nas.ovh pingvin 192.168.2.57 psitransfer.photos-nas.ovh psitransfer diff --git a/lan_1.list b/lan_1.list deleted file mode 100644 index 8885e8f..0000000 --- a/lan_1.list +++ /dev/null @@ -1,23 +0,0 @@ -192.168.2.57 navidrome.photos-nas.ovh navidrome -192.168.2.57 ds923.photos-nas.ovh dsm -192.168.2.57 maloja.photos-nas.ovh maloja -192.168.2.57 photos.photos-nas.ovh photos -192.168.2.57 change.photos-nas.ovh changedetection -192.168.2.57 search.photos-nas.ovh searxng -192.168.2.57 vault.photos-nas.ovh vaultwarten -192.168.2.57 menu.photos-nas.ovh heimdall -192.168.2.57 notif.photos-nas.ovh nfty -192.168.2.57 uptime.photos-nas.ovh uptime-kuma -192.168.2.57 dozzle.photos-nas.ovh dozzle -192.168.2.57 gotify.photos-nas.ovh gotify -192.168.2.57 igotify.photos-nas.ovh gotify-api -192.168.2.57 tautulli.photos-nas.ovh tautulli -192.168.2.57 glances.photos-nas.ovh glances -192.168.2.57 test.photos-nas.ovh test -192.168.2.57 paperlessngx.photos-nas.ovh paperlessngx -192.168.2.57 docspell.photos-nas.ovh docspell -192.168.2.1 asus.photos-nas.ovh asus -192.168.2.1 www.asusrouter.com et12 -192.168.2.116 pihole1.photos-nas.ovh dietpi1 -192.168.2.216 pihole2.photos-nas.ovh dietpi2 - diff --git a/lan_2.list b/lan_2.list deleted file mode 100644 index b324374..0000000 --- a/lan_2.list +++ /dev/null @@ -1,23 +0,0 @@ -192.168.2.57 navidrome.photos-nas.ovh navidrome -192.168.2.57 ds923.photos-nas.ovh dsm -192.168.2.57 maloja.photos-nas.ovh maloja -192.168.2.57 photos.photos-nas.ovh photos -192.168.2.57 change.photos-nas.ovh changedetection -192.168.2.57 search.photos-nas.ovh searxng -192.168.2.57 vault.photos-nas.ovh vaultwarten -192.168.2.57 menu.photos-nas.ovh heimdall -192.168.2.57 notif.photos-nas.ovh nfty -192.168.2.57 uptime.photos-nas.ovh uptime-kuma -192.168.2.57 dozzle.photos-nas.ovh dozzle -192.168.2.57 gotify.photos-nas.ovh gotify -192.168.2.57 igotify.photos-nas.ovh gotify-api -192.168.2.57 tautulli.photos-nas.ovh tautulli -192.168.2.57 glances.photos-nas.ovh glances -192.168.2.57 paperlessngx.photos-nas.ovh paperlessngx -192.168.2.57 docspell.photos-nas.ovh docspell -192.168.2.57 test.photos-nas.ovh test -192.168.2.1 asus.photos-nas.ovh asus -192.168.2.1 www.asusrouter.com et12 -192.168.2.116 pihole1.photos-nas.ovh dietpi1 -192.168.2.216 pihole2.photos-nas.ovh dietpi2 - diff --git a/sync_pihole_lan.sh b/sync_pihole_lan.sh index 84c7707..a2c9b36 100755 --- a/sync_pihole_lan.sh +++ b/sync_pihole_lan.sh @@ -14,32 +14,90 @@ green="\033[0;32m" reset="\033[0m" +absent=() +delete=() +domain=".photos-nas.ovh" +not_required=("drive" "files" "gitea" "home-assistant" "homebridge" "portainer" "wg" "yatch") +not_used=("ds916" "musiiic" "tunes" "notif" "pastefy") + +pihole1=192.168.2.116 +pihole2=192.168.2.216 + +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 + +# Sauvegarde du lan.list + echo -e "${bold}Backup ${italic}lan.list...${reset}" -ssh -p51322 root@192.168.2.116 "gzip -c cat /etc/pihole/lan.list" > lan.list.1.gz -echo "--" -ssh -p51522 root@192.168.2.216 "cat /etc/pihole/lan.list" > lan.list.2.gz +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 "\n${red}Pihole2 is unreachable !'${reset}" -cp lan.list.bak /tmp/lan.list -bzip2 /tmp/lan.list # lan.list.bz2 -mv /tmp/lan.list.bz2 . -cp lan.list lan.list.bak +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 +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 "$nb_dynhost dynhost found !" - -# nano /etc/pihole/lan.list - -#ssh -p51322 root@192.168.2.116 "cat /etc/pihole/lan.list" > /Users/bruno/Documents/Scripts/pihole/lan_1.list -#ssh -p51522 root@192.168.2.216 "cat /etc/pihole/lan.list" > /Users/bruno/Documents/Scripts/pihole/lan_2.list +echo "$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 + echo "" +fi + + +# Ajout des DynHOST de OVH (Zones DNS) dans les 2 lan.list (pihole1 & pihole2) # Zones DNS -> photos-nas.ovh -> Historique des zones -absent=() -not_required=("drive" "files" "gitea" "home-assistant" "homebridge" "portainer" "wg" "yatch") -not_used=("ds916" "musiiic" "tunes") +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') while IFS= read -r line; do @@ -47,7 +105,7 @@ 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 + if [[ ! " ${not_required[*]} " =~ " $dynhost " ]] && [[ ! " ${not_used[*]} " =~ " $dynhost " ]]; then absent+=(${dynhost}) fi fi @@ -57,72 +115,62 @@ done <<< "$dynhost_list" echo echo -e "${bold}Update ${italic}lan.list...${reset}" echo -e "${green}Not required: ${not_required[@]}${reset}" -#echo echo -e "${italic}Not used: ${not_used[@]}${reset}" -#echo -echo -e "${red}Absent: ${absent[@]}${reset}" -#echo +echo -e "${red}Missing: ${absent[@]}${reset}" + if [ ${#absent[@]} -ge 1 ]; then - [ -f temp_file.list ] && rm temp_file.list + [ -f /tmp/temp_file.list ] && rm /tmp/temp_file.list for val in ${absent[@]} do - echo -e "192.168.2.57 ${val}.photos-nas.ovh ${val}" >> temp_file.list + echo -e "192.168.2.57 ${val}.photos-nas.ovh ${val}" >> /tmp/temp_file.list done - cat temp_file.list >> lan.list + cat /tmp/temp_file.list >> lan.list +else + echo -e "\n${green}No new dynhost !${reset}" fi cat lan.list | sort -k2 > lan.list.sorted mv lan.list.sorted lan.list echo -echo -e "${red}Display ${italic}lan.list...${reset}" +echo -e "${bold}Display ${italic}lan.list...${reset}" cat lan.list nb_dynhost=$(awk 'END { print NR }' lan.list) -echo "$nb_dynhost dynhost !" +echo -e "$nb_dynhost dynhost !\n" + -[ ${#absent[@]} -eq 0 ] && echo -e "\n${green}No new dynhost !${reset}" a=$(echo -e "Do you want to export ${italic}lan.list${reset} to ${bold}pihole1${reset} and ${bold}pihole2${reset} ? (y/n)") read -p "$a" choice if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then - #curl -Is https://google.com | head -1 | grep 200 1>/dev/null - #curl -Is https://1.1.1.1 | head -1 | grep 302 1>/dev/null - 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 + 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}" fi - - #echo "Export to pihole1..." - #cat lan.list | ssh -p51322 root@192.168.2.116 'cat > /etc/pihole/lan.list && pihole restartdns' - #[ $? != 0 ] && echo -e "${red}Error during transfer to pihole1 (192.168.2.116)${reset}" || echo -e "${green}Ok${reset}" - echo "Export to pihole1..." - cat lan.list | ssh -p51322 root@192.168.2.116 'cat > /etc/pihole/lan.list' - [ $? != 0 ] && echo -e "${red}Error during transfer to pihole1 (192.168.2.116)${reset}" || echo -e "${green}Ok${reset}" - - echo "Restarting dns on pihole1..." - ssh -p51322 root@192.168.2.116 'pihole restartdns' - [ $? != 0 ] && echo -e "${red}Error during restarting dns on pihole1 (192.168.2.116)${reset}" || echo -e "${green}Ok${reset}" - echo "Export to pihole2..." - cat lan.list | ssh -p51522 root@192.168.2.216 'cat > /etc/pihole/lan.list' - [ $? != 0 ] && echo -e "${red}Error during transfer to pihole2 (192.168.2.216)${reset}" || echo -e "${green}Ok${reset}" - echo "Restarting dns on pihole2..." - ssh -p51522 root@192.168.2.216 'pihole restartdns' - [ $? != 0 ] && echo -e "${red}Error during restarting dns on pihole2 (192.168.2.216)${reset}" || echo -e "${green}Ok${reset}" + 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}" + fi +else + echo -e "OK, let's continue..." fi -[ -f temp_file.list ] && rm temp_file.list +[ -f /tmp/temp_file.list ] && rm /tmp/temp_file.list [ -f lan.list.sorted ] && rm lan.list.sorted - - -#cat ~/Documents/Scripts/pihole/lan.list - -# dockge syno-dashboard yacy