14-09-2025

Recherche des sous-domaines supplementaires dans lan.list (d'après subdomains.photos-nas.ovh.txt)
This commit is contained in:
2025-09-14 18:27:41 +02:00
parent 1e220cd809
commit 0cb49accf7
3 changed files with 167 additions and 53 deletions

View File

@@ -26,17 +26,26 @@ reset="\033[0m"
absent=()
delete=()
extra=()
domain=".photos-nas.ovh"
not_required=("drive" "files" "gitea" "home-assistant" "homebridge" "portainer" "tunes" "wg" "www" "yatch")
not_required=("drive" "files" "gitea" "home-assistant" "homebridge" "portainer" "tunes" "wg" "www")
not_used=("ds916" "musiiic" "notif")
pihole1=192.168.2.116
pihole2=192.168.2.216
ScriptPath="$(readlink -f "$0")" # /Users/bruno/Documents/Scripts/bashbirds/bashbirds.sh
ScriptWorkDir="$(dirname "$ScriptPath")" # /Users/bruno/Documents/Scripts/bashbirds
echo -e "${greenbold}Sync_pihole_lan.sh synchronise the /etc/pihole/lan.list file with your Subdomains ...${reset}\n"
echo -e " For Pihole 6:"
echo -e " - edit Pihole preferences (nano /etc/pihole/pihole.toml)"
echo -e " - set etc_dnsmasq_d = true ### CHANGED, default = false"
echo
echo
: << 'COMMENTS'
COMMENTS
cat < /dev/null > /dev/tcp/1.1.1.1/53
if [[ $? -ne 0 ]]; then
echo -e "\n${red}No Internet connection !${reset}"
@@ -48,37 +57,37 @@ fi
echo -e "${bold}Backup ${italic}lan.list...${reset}"
# 1. 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}"
ping -q -c1 "$pihole1" &>/dev/null && ssh -p51322 root@"$pihole1" "cat /etc/pihole/lan.list" > $ScriptWorkDir/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" > $ScriptWorkDir/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
if [ -f "$ScriptWorkDir/lan.list.bak" ]; then
cp "$ScriptWorkDir/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
if [ -f $ScriptWorkDir/lan.list ]; then
cp $ScriptWorkDir/lan.list $ScriptWorkDir/lan.list.bak
elif [ -f $ScriptWorkDir/lan.list.1 ]; then
cp $ScriptWorkDir/lan.list.1 $ScriptWorkDir/lan.list
elif [ -f $ScriptWorkDir/lan.list.2 ]; then
cp $ScriptWorkDir/lan.list.2 $ScriptWorkDir/lan.list
else
echo "Error ! No lan.list file !"
exit
fi
COMMENTS
nb_dynhost=$(awk 'END { print NR }' lan.list)
nb_dynhost=$(awk 'END { print NR }' $ScriptWorkDir/lan.list)
echo -e "\n$nb_dynhost dynhost found in lan.list !"
# 2. Suppression des 'not_required' et 'not_used' dans le lan.list
echo -e "${bold}\nFind extra dynhost in ${italic}lan.list...${reset}"
echo -e "${bold}\nFind not_used and not_required dynhost in ${italic}lan.list...${reset}"
while IFS= read -r line; do
@@ -89,7 +98,7 @@ while IFS= read -r line; do
fi
fi
done < lan.list
done < $ScriptWorkDir/lan.list
if [ ${#delete[@]} -ge 1 ]; then
@@ -110,7 +119,7 @@ if [ ${#delete[@]} -ge 1 ]; then
echo "$nb_dynhost dynhost found in lan.list !"
fi
else
echo "None"
echo -e "${green}None !${reset}"
fi
@@ -119,12 +128,12 @@ fi
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)
dynhost_list=$(cat $ScriptWorkDir/subdomains.photos-nas.ovh.txt)
while IFS= read -r line; do
dynhost=$(echo "${line}" | awk '{print $1}')
grep -q $dynhost lan.list
grep -q $dynhost $ScriptWorkDir/lan.list
if [ $? != 0 ]; then
if [[ ! " ${not_required[*]} " =~ " $dynhost " ]] && [[ ! " ${not_used[*]} " =~ " $dynhost " ]]; then
absent+=(${dynhost})
@@ -154,29 +163,70 @@ if [ ${#absent[@]} -ge 1 ]; then
cat /tmp/temp_file.list
# On ajoute le /tmp/temp_file.list au lan.list
cat /tmp/temp_file.list >> lan.list
cat /tmp/temp_file.list >> $ScriptWorkDir/lan.list
# On trie le lan.list
cat lan.list | sort -k2 > lan.list.sorted
mv lan.list.sorted lan.list
cat $ScriptWorkDir/lan.list | sort -k2 > $ScriptWorkDir/lan.list.sorted
mv $ScriptWorkDir/lan.list.sorted $ScriptWorkDir/lan.list
else
echo -e "${green}No new dynhost !${reset}"
fi
# 4. Recherche des sous-domaines supplementaires dans lan.list (d'après subdomains.photos-nas.ovh.txt)
echo -e "${bold}\nRemove extra dynhost in ${italic}lan.list...${reset}"
lan_list=$(cat $ScriptWorkDir/lan.list)
while IFS= read -r line; do
dynhost=$(echo "${line}" | awk '{print $2}' | awk -F"." '{print $1}')
grep -q $dynhost $ScriptWorkDir/subdomains.photos-nas.ovh.txt
if [ ! $? != 0 ]; then
echo "${line}" >> /tmp/extra.txt
else
extra+=(${dynhost})
fi
if [[ "$dynhost" == *www* ]]; then
echo "${line}" >> /tmp/extra.txt
#extra=( "${extra[@]/$dynhost}" )
# on supprime www et on reconstruit le tableau
new_array=()
for value in "${extra[@]}"
do
[[ $value != $dynhost ]] && new_array+=("$value")
done
extra=("${new_array[@]}")
unset new_array
fi
done <<< "$lan_list"
if [ ${#extra[@]} -ge 1 ]; then
echo "${extra[@]}"
else
echo -e "${green}No extra dynhost !${reset}"
fi
cp /tmp/extra.txt $ScriptWorkDir/lan.list
[ -f /tmp/extra.txt ] && rm /tmp/extra.txt
echo
echo -e "${bold}Display ${italic}lan.list. Please verify IP and subdomains.${reset}"
cat lan.list
cat $ScriptWorkDir/lan.list
nb_dynhost=$(awk 'END { print NR }' lan.list)
nb_dynhost=$(awk 'END { print NR }' $ScriptWorkDir/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
nano -l $ScriptWorkDir/lan.list
fi
@@ -184,7 +234,7 @@ fi
: << 'COMMENTS2'
echo "toto"
COMMENTS2
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
@@ -192,12 +242,16 @@ 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'
cat $ScriptWorkDir/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}"
# pihole 5
#echo "Restarting dns on pihole1..."
#ssh -p51322 root@"$pihole1" 'pihole restartdns'
# pihole 6
echo "Update the lists and flush the cache without restarting the DNS server on pihole1..."
ssh -p51322 root@"$pihole1" 'pihole reloaddns'
[ $? != 0 ] && echo -e "${red}Error during reloading dns on pihole1 ($pihole1)${reset}" || echo -e "${green}Ok${reset}"
else
echo -e "\n${red}Pihole1 is unreachable !'${reset}"
fi
@@ -205,20 +259,24 @@ if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
if ping -q -c1 "$pihole2" &>/dev/null; then
echo "Export to pihole2..."
cat lan.list | ssh -p51522 root@"$pihole2" 'cat > /etc/pihole/lan.list'
cat $ScriptWorkDir/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}"
# pihole 5
#echo "Restarting dns on pihole2..."
#ssh -p51522 root@"$pihole2" 'pihole restartdns'
# pihole 6
echo "Update the lists and flush the cache without restarting the DNS server on pihole2..."
ssh -p51522 root@"$pihole2" 'pihole reloaddns'
[ $? != 0 ] && echo -e "${red}Error during reloading dns on pihole2 ($pihole2)${reset}" || echo -e "${green}Ok${reset}"
else
echo -e "n${red}Pihole2 is unreachable !'${reset}"
echo -e "\n${red}Pihole2 is unreachable !'${reset}"
fi
else
echo -e "OK, let's continue..."
fi
COMMENTS2
# 5. On compare la liste principale des sous-domaines subdomains.photos-nas.ovh.txt avec la zone photos-nas.ovh sur ovh (via l'API)
@@ -232,7 +290,7 @@ TIME=$(curl -s https://api.ovh.com/1.0/auth/time)
ZONE_FILE="zone.photos-nas.ovh"
ZONE="photos-nas.ovh"
source ovh_secrets.txt
source $ScriptWorkDir/ovh_secrets.txt
export_zone() {
HTTP_METHOD="GET"
@@ -327,8 +385,8 @@ export_zone
#ovh_cname_list=$(cat photos-nas.ovh_dns_data.txt | grep "CNAME" | sed '1d')
ovh_cname_list=$(cat $ZONE_FILE)
subdomains_list=$(cat subdomains.photos-nas.ovh.txt)
ovh_cname_list=$(cat $ScriptWorkDir/$ZONE_FILE)
subdomains_list=$(cat $ScriptWorkDir/subdomains.photos-nas.ovh.txt)
extra_ovh=()
absent_ovh=()
@@ -337,7 +395,7 @@ echo -e "${bold}\nFind extra CNAME records in zone ${italic}$ZONE${reset}..."
while IFS= read -r line; do
dynhost=$(echo "${line}" | awk '{print $1}')
grep -q $dynhost subdomains.photos-nas.ovh.txt
grep -q $dynhost $ScriptWorkDir/subdomains.photos-nas.ovh.txt
if [ $? != 0 ]; then
if [[ ! " ${not_required[*]} " =~ " $dynhost " ]] && [[ ! " ${not_used[*]} " =~ " $dynhost " ]]; then
@@ -379,7 +437,7 @@ echo -e "${bold}\nFind missing CNAME records in zone ${italic}$ZONE${reset}..."
while IFS= read -r line; do
dynhost=$(echo "${line}" | awk '{print $1}')
grep -q $dynhost zone.photos-nas.ovh
grep -q $dynhost $ScriptWorkDir/zone.photos-nas.ovh
if [ $? != 0 ]; then
if [[ ! " ${not_required[*]} " =~ " $dynhost " ]] && [[ ! " ${not_used[*]} " =~ " $dynhost " ]]; then
@@ -416,7 +474,12 @@ fi
# 6. On supprime les fichiers temporaires
[ -f /tmp/temp_file.list ] && rm /tmp/temp_file.list
[ -f lan.list.sorted ] && rm lan.list.sorted
[ -f $ScriptWorkDir/lan.list.sorted ] && rm $ScriptWorkDir/lan.list.sorted
# 7. fin
echo -e "\n${greenbold}Fin !${reset}"
# https://www.nas-forum.com/forum/topic/80160-multiples-dynhost-ovh/#comment-1319505612